0

I have a class that extends JPanel and when I try to set one specific instance of the class to visible, all other classes expand aswell. Ideas?

Before 'two' CENTER is set visible: Before 'two' opens

After 'two' CENTER is set visible: enter image description here

notice that all other instances open. Here is the best I can do with my code since it is very abstract:

Call -

display.mainScrollPane.getMainPanel().getMachineListPanel().getMachine("two").panel.showPanel();
BinaryShrub
  • 346
  • 1
  • 3
  • 14
  • 6
    How did you create these objects? Most likely what's happening is that you have several references all pointing to the same object. –  Aug 03 '12 at 15:32
  • 2
    Are you using a static variable? – Guillaume Polet Aug 03 '12 at 15:33
  • I'm always getting a thrills when I see invocations like this `display.mainScrollPane.getMainPanel().getMachineListPanel().getMachine("two").panel.showPanel();` and I must inspect that code. Try to redesign your approach. – Xeon Aug 03 '12 at 16:06
  • 3
    Post a SSCCE for proper help – Sanyam Goel Aug 03 '12 at 16:54
  • 1
    A `GridLayout` would produce that effect. Use a `BoxLayout` instead. (And if that does not solve the problem, post an SSCCE.) – Andrew Thompson Aug 03 '12 at 23:17

2 Answers2

0

Absent your sscce it's hard to say.

  • If you are using BorderLayout, note that components in the CENTER will expand to fill the available space. Alternatively, consider BoxLayout, illustrated here and here.

  • If you are adding components after pack() and setVisible(), be sure to validate() and repaint() as suggested here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

The cause was due to the fact that the MachineListPanel implements GridLayout. By definition, GridLayout forces all elements to be the same height and width -- ie. when one of the components' CENTER was set visible, the space also had to be allocated for all other elements in the layout.

BinaryShrub
  • 346
  • 1
  • 3
  • 14