today i’m having a little problem, that probably is nothing for pros here 🙂
I want to have my Swing compononents in one horizontal line. I used FlowLayout and changed size of components on componentResized() according to the frame size, but it often bugged (sometimes the last component was placed in next row)… I’ve decided to use BoxLayout, but on that code:
down=new JPanel(new BoxLayout(down,BoxLayout.X_AXIS));
down.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
down.add(Box.createHorizontalGlue());
down.setPreferredSize(new Dimension(300,35));
it crashes with:
Exception in thread "AWT-EventQueue-0" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(Unknown Source)
at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
at javax.swing.BoxLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at ButtonFrame.<init>(chat.java:278)
at chat$1.run(chat.java:20)
aso...
I dont know what to do, maybe i can make FlowLayout unable to make 2nd row, or make box layout work?
Thanks for any replies!
Copy-pasted from the Swing tutorial about
BoxLayoutsSee how the panel is first created without any layout, and then the layout is set and created with the existing panel. This is different with your
Adjusting this line (making it two separate statements as in the example) will remove the exception.
Oh yeah, a
BoxLayoutshould allow to fulfill your requirement