0

I have a GridLayout(3,2) as follows with 2 JLabels, 2 JTextFields and a JButton. I add them as shown in the pic or code. Everything is just fine but the JTextField size is too big and I want it to be as shown by the red lines I've drawed. I have tried saying jtf3.setPreferredSize( new Dimension( x, y ) ); but it didn't change the dimension at all. One other solution was to make the GridLayout somewhat GridLayout(3,2,1,50) for example (by adding 50) but that moves the JLabels way top too... I just want to be exactly as shown in the pic... Any ideas? Thanks a lot

enter image description here

JPanel copying_panel = new JPanel();
copying_panel.setLayout(new GridLayout(3, 2));
copying_panel.setBackground(new Color(200, 221, 242));
JLabel jl4 = new JLabel("From:", SwingConstants.CENTER);
JTextField jtf3 = new JTextField();
JLabel jl5 = new JLabel("To:", SwingConstants.CENTER);
JTextField jtf4 = new JTextField();
JLabel jl6 = new JLabel();
JButton jb2 = new JButton("Go");

copying_panel.add(jl4);
copying_panel.add(jtf3);
copying_panel.add(jl5);
copying_panel.add(jtf4);
copying_panel.add(jl6);
copying_panel.add(jb2);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3549987
  • 23
  • 1
  • 1
  • 4

1 Answers1

3

That's how GridLayout works, it provides equal space to all the components. Instead, consider using a GridBagLayout instead.

See How to Use GridBagLayout for more details

JPanel copying_panel = new JPanel();
copying_panel.setLayout(new GridBagLayout());
copying_panel.setBackground(new Color(200, 221, 242));
JLabel jl4 = new JLabel("From:", SwingConstants.CENTER);
JTextField jtf3 = new JTextField(10);
JLabel jl5 = new JLabel("To:", SwingConstants.CENTER);
JTextField jtf4 = new JTextField(10);
JButton jb2 = new JButton("Go");

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
copying_panel.add(jl4, gbc);

gbc.gridy++;
copying_panel.add(jl5, gbc);

gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
gbc.gridy = 0;
copying_panel.add(jtf3, gbc);

gbc.gridy++;
copying_panel.add(jtf4, gbc);

gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridy++;
copying_panel.add(jb2, gbc);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • JLabels and JTextFields are way too short. Should I just setPreferedSize on them? – user3549987 Dec 10 '14 at 00:06
  • Hell no. You supply sizing hints to the text fields via the `columns` property, as demonstrated in the above example, `new JTextField(10)`. You can use `GridBagConstraint#insets` to adjust the amount of padding around each component. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) for more details. I'd be careful about separating labels from fields over a long distance, this can "disconnect" the two components, confusing he user – MadProgrammer Dec 10 '14 at 00:08
  • You can also use the "weightx" value of gridbagconstraints to adjust the size of the textfield according to leftover space. In this case, you would not set the columns... just new JTextField() is fine. There will need to be leftover space, though, or your text field will have a zero width. If only the textfield should expand, that would have a weightx of 1, and all other items would have a weightx of 0. (all weights should add up to 1) – pcalkins Jul 28 '17 at 23:45
  • @pcalkins That would depend entirely on what you want to achieve, using `weightx` would also mean, that if you sized the component to the full screen, the text field would occupy the entire remaining space of the component, which may not be what you want to achieve, but seeding the textfields with a column value, we also seed them with a preferred size, but, again, it all comes down to what you want to achieve – MadProgrammer Jul 28 '17 at 23:52
  • True. I should have also mentioned, you'll want to set GridBagConstraints.fill or it'll also be zero width... I've been using a defined width scrollpane with viewport set to a panel that has a gridbaglayout... in my case I need certain buttons lined up, and the text fields to fill remaining space... I set my fill for the textfields to be GridBagConstraints.HORIZONTAL and they take up all the weightx... I'm still learning to deal with Swing myself, so I may come across some more problems with this method. – pcalkins Jul 29 '17 at 16:13
  • @pcalkins With the text fields defined the way are, they’ll have a defined width – MadProgrammer Jul 29 '17 at 21:24