4

I try to put two jpanels side by side, but in this moment i can not do what i want I have this code,

this.videoPanel= new JPanel();
this.videoPanel.setBackground(new Color(102, 102, 102));
this.videoPanel.setPreferredSize(new Dimension(320, 240));
this.videoPanel.setLayout(new BoxLayout(this.videoPanel, 1));

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);

layout.setHorizontalGroup(
        layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addComponent(this.videoPanel, 10, 400, 500)));

layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        .addComponent(this.videoPanel, 10, 400, 500)));

pack();

` I try to put one more jpanel:

 this.videoPanel1.setBackground(new Color(102, 102, 102));
 this.videoPanel1.setPreferredSize(new Dimension(320, 240));
 this.videoPanel1.setLayout(new BoxLayout(this.videoPanel1, 2));

 GroupLayout layout = new GroupLayout(getContentPane());
 getContentPane().setLayout(layout);



 layout.setHorizontalGroup(
         layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addComponent(this.videoPanel1, 10, 400, 500)
         .addComponent(this.videoPanel, 10, 400, 500)));

 layout.setVerticalGroup(
         layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
         .addComponent(this.videoPanel1, 10, 400, 500)
         .addComponent(this.videoPanel, 10, 400, 500)));
      pack();

this code don't work and also does not give error.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
FCoelho
  • 165
  • 1
  • 4
  • 11
  • Don't use a GroupLayout it is mainly used by an IDE because it is confusing to use. You can easily use a FlowLayout or GridBagLayout or BoxLayout. – camickr Jul 31 '13 at 17:24

1 Answers1

8

If I understand your question correctly, you want to place 2 JPanel side by side. Look at the Layout GridLayout.

You'd want setLayout(new GridLayout(1,2)); which says 1 Row, 2 Col


package SSCCE;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SideBYSide extends JFrame{

    public static void main(String[] args) {
        new SideBYSide();
    }

    public SideBYSide(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(100, 75);
        this.setLayout(new BorderLayout());
        this.setVisible(true);

        JPanel container = new JPanel();
        JPanel panelOne = new JPanel();
        JPanel panelTwo = new JPanel();

        panelOne.add(new JLabel("1"));
        panelTwo.add(new JLabel("2"));

        container.setLayout(new GridLayout(1,2));
        container.add(panelOne);
        container.add(panelTwo);

        this.add(container);
    }

 }
Kryptos
  • 542
  • 2
  • 8
  • 18
  • Nice example, it's always a good practice, to first add all components to the container and then set its visible property to true, just after calling pack on it :-) – nIcE cOw Jul 31 '13 at 18:05
  • 1
    FCoelho NP @NIcE cOW I know, I normally do... I don't really know why I didn't this time. Brain fart I guess. – Kryptos Jul 31 '13 at 18:48