2

I am using WindowBuilder Pro for eclipse, and I would like to have two Jpanels that perfectly overlap each other. I would then be able to toggle their visibilty based on the selection of a combox box. When I try and acheive this in the gui builder, the first panel gets displaced by the second panel. And advice please?

vcsjones
  • 138,677
  • 31
  • 291
  • 286
pingu
  • 8,719
  • 12
  • 50
  • 84

3 Answers3

4

It is possible using groupLayout, according to the tutorial .

What you must do is add the components to a mother JPanel , and set that panel to use GroupLayout.

Then add the components to the layout as a ParallelGroup in both the horizontal and vertical spacing. This means they will occupy the same X and Y space. Then disable/enable as needed, hiding the JPanels as well.

I believe the way it would work is so:

        JPanel panel1, panel2, panel3;
        //initialize panel3, etc
        panel1=new JPanel();
        panel2 = new JPanel();
        panel1.add(new JTextField("Panel1"));
        panel2.add(new JTextField("PANEL2"));
        
        groupLayout = new GroupLayout(panel3);
        
        panel3.setLayout(groupLayout);
        
        groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(panel1)
            .addComponent(panel2)
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(panel1)
            .addComponent(panel2)
        );
        panel1.setEnabled(false);
        panel1.setVisible(false);

then add a jCheckBox with an ActionPerformed method containing:

if(panel1.isEnabled()) {
    panel1.setEnabled(false);
    panel1.setVisible(false);
    panel2.setEnabled(true);
    panel2.setVisible(true);
    
}else
 if(panel2.isEnabled()) {
    panel2.setEnabled(false);
    panel2.setVisible(false);
    panel1.setEnabled(true);
    panel1.setVisible(true);
    
}

That produced the desired behaviour for me. You should be able to switch the JComboBox for the JCheckBox fairly easily.

EDIT: Removed the necessity of having "Jpanel of their own". That should not be the case, and the above method allows you to get the benefits of both GroupLayout and CardLayout.

Community
  • 1
  • 1
Sheriff
  • 495
  • 4
  • 16
  • 1
    Why reinvent the wheel. A CardLayout is designed for this purpose. – camickr Dec 22 '11 at 01:15
  • Thanks for this Sheriff, the problem with this is that it is very difficult to visually design your forms, unless I am doing it wrong. – pingu Dec 22 '11 at 09:43
  • 1
    @pingu no worries. Card layout seems to be what you are looking for. I can see some situations where This would be the preferred method, but overall card layout is designed for this. – Sheriff Dec 22 '11 at 14:05
3

I would like to have two Jpanels that perfectly overlap each other. I would then be able to toggle their visibilty based on the selection of a combox box

See: How to Use Card Layout for an example that does exactly this.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

I would like to have two Jpanels that perfectly overlap each other.

I believe the CardLayout is there exactly for that reason.

Basically, you can nest different panels or 'Cards' using the CardLayout and set the appropriate card to be displayed programmatically (on some event).

Optimized Coder
  • 189
  • 2
  • 8
  • -1 this answer was given 30 minutes earlier. There is no need to clutter the forum with duplicate answers. – camickr Dec 22 '11 at 01:17