0

Please consider this Java code fragment:

Box buttonBox = Box.createHorizontalBox();
buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
buttonBox.add(new JTextField());

Box paneBox = Box.createVerticalBox();
paneBox.add(buttonBox);
paneBox.add(new JScrollPane(jList));

For some reason i don't get, the JTextField takes up most of the screen. The jList isn't even visible anymore. I would like to know why and how to fix it.

  1. When i comment the line with the JTextField, it looks fine (except no JTextField, of course). Why does the JCheckBox and the JLabel not get ridiculously big? What is the purpose of a one-line JTextField being able to take up almost the whole screen?

  2. Most people suggest to set the size of the JTextField field to my needs. However, i read that i should not call these methods, but let the LayoutManager take care of it. Now what is the most elegant solution to prevent the JTextField from becoming so big?

3 Answers3

1

Try using the setPreferredSize method like this:

textFieldName.setPreferredSize(new Dimension(x, y));

Obviously input the values for x and y that you wish the JTextField size to be

Schonge
  • 332
  • 2
  • 6
  • 17
  • Unfortunately, setColumns has no effect. However, i'm more concerned about the height of the JTextField, which is FAR more then one line of text would need. setColumns would only influence the width, if i understand correctly. – user3297096 May 20 '14 at 13:51
  • OK my apologies I'll edit my answer to something that may work for you – Schonge May 20 '14 at 14:05
  • [This thread](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) says i should never use this function. I would like to know the proper way to format the JTextField. – user3297096 May 20 '14 at 14:38
  • I hadn't come across that before, from what I read in it though I'm guessing that using a LayoutManager is the way to go. I have no experience with doing that so perhaps someone else will be able to elaborate some more on the topic. – Schonge May 20 '14 at 14:44
0

You can avoid this problem by using a simple JPanel() instead of a Box for the horinzontal one. And after just set the prefered size to your JTextField.

//Box buttonBox = Box.createHorizontalBox();
JPanel buttonBox = new JPanel(); 

buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
JTextField jt = new JTextField();
jt.setPreferredSize(new Dimension(100,25));
buttonBox.add(jt);

EDIT

Btw the defaut Layout of a JPanel is the FlowLayout which place components from left to right and with centered alignement by default. You can still change the alignement. For example:

 JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.LEFT));

You can also change the gap between each components. There is some other Layout like the BorderLayout or GridLayout / GridBagLayout. I let you search on Google for more information about them.

enter image description here

See in yellow the JPanel, in red the VerticalBox

beny1700
  • 284
  • 1
  • 4
  • 10
  • The call to setPreferredSize is unneccesary, as the JTextField doesn't become huge with this layout. According to [this thread](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), these methods should be avoided anyway. However, the JPanel itself is getting too big, it takes up half the screen, the most of it is empty. Therefore, this doesn't cut it for me either. – user3297096 May 21 '14 at 09:51
  • JPanel doesn't get too big, I don't understand your problem here. I just edited my post to show you a screenshot. – beny1700 May 22 '14 at 11:38
0
Box box = Box.CreateVerticalBox();

Use box.setMaximumSize(Dimension(160,80)) and box.setMinimumSize(Dimension(160,80)) then it will work :D If you need a strut for your layout, put it in between boxes.