Java Swing can't add multiple panels to panel

13,597

As you need panel contain two panel? try this

JPanel top = new JPanel(new GridLayout(1,1))
JPanel left = new JPanel();
JPanel right = new JPanel();
top.add(left);
top.add(right);
JFrame frame = new JFrame();
frame.add(top);
frame.setSize(400,400);
frame.setVisible(true);
Share:
13,597
B13ZT
Author by

B13ZT

Updated on July 28, 2022

Comments

  • B13ZT
    B13ZT over 1 year

    I'm trying to add 2 panels to one panel. Now the problem is, when I add the first or the second one alone, I can see them in the panel. But when I add both off them (put //add(panel2, BorderLayout.SOUTH); out of comment) I don't see any of them. The Weblabel("Test") is always showing. Now both panels have a onclick method and when you can't see them (if they are both added) the onclick still works if you click where the panels are supposed to be.

    I already tried other layoutmanagers but without succes. Does anyone know where this problem could be comming from?

    setLayout(new BorderLayout());
    add(panel1,BorderLayout.NORTH);
    add(new WebLabel("Test"), BorderLayout.CENTER);
    //add(panel2,BorderLayout.SOUTH);
    

    I initialize both panels in my constructor.

    public MultipleFloorPlanEntityPanel(int xCoordinate, int yCoordinate,   FloorPlanEntityPanel panel1, FloorPlanEntityPanel panel2){
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        this.panel1 = panel1;
        this.panel2 = panel2;
        layoutComponents();
    }
    

    Solved: reinitialized the extra panels and that worked :)