0

So I have some code like this inside init() for an applet:

layout = new BorderLayout();
setLayout(layout);

northPanel = new JPanel(new FlowLayout());

northPanel.add(inputDropDown);
northPanel.add(lowBoundLabel);
northPanel.add(lowBoundField);
northPanel.add(highBoundLabel);
northPanel.add(highBoundField);
northPanel.add(new JLabel("using"));
northPanel.add(categoriesField);
northPanel.add(new JLabel("categories"));
northPanel.add(showTotalsBox);
northPanel.add(refreshButton);

add(northPanel, BorderLayout.NORTH);

Now when I test it, all of the elements are in a straight line and do not wrap around when there is not enough space. I even made sure to specify that the panel is FlowLayout (even though it's the default) and it didn't change anything.

Shouldn't they wrap instead of just going off the screen? What's going on? I came up with a temporary solution by changing the northPanel to BorderLayout, splitting up these elements into to separate panels and adding them to North and South. However, the elements just disappear off the screen without the necessary space in this method so I'd rather have them wrap around.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Watabou
  • 51
  • 1
  • 3
  • 9

1 Answers1

2

This is actually exactly how FlowLayout works, annoying isn't it...

Take a look at WrapLayout instead...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • "The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows." Taken straight from Oracle docs -_- They are basically doing the same thing as I am in this code here and it's wrapping as it should when I test it: [link](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/FlowLayoutDemoProject/src/layout/FlowLayoutDemo.java) – Watabou May 30 '13 at 01:41
  • I'm not sure that's why I was asking here lol. My code doesn't wrap when I test it but when I test their code it does. I'm not sure what is different. I can look into WrapLayout. – Watabou May 30 '13 at 01:45
  • Check `WrapLayout`, it may provide you details about the problem you are facing...I'd also look into the components you are adding and determine if they are actually returning a preferred size or if the `FlowLayout` is using their minimum size instead... – MadProgrammer May 30 '13 at 01:48
  • +1, `If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows` - The preferred size is based on a single row. So if you add the panel to the NORTH of a BorderLayout you will only ever see a single row. (ie. the components are positioned on a second row, but because they are outside the height of the panel you will never see them). If you add it to the CENTER and there is space available then you will see the wrapped components. – camickr May 30 '13 at 02:14
  • Ah interesting. That makes sense. And is also consistent with the Oracle sample code as the components added to CENTER wrap but the few added to SOUTH just kinda disappear. – Watabou May 30 '13 at 02:17