1

I would like to have in my app text fields that span multiple rows. It seems to be no problem to get the desired result in Eclipse design viewer using GridBagLayout:

enter image description here

However, when I export it to jar, I see the window like this:

enter image description here

My code:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class TestFrame extends JFrame {
    GridBagConstraints constraints;
    JTextField text0;

    public TestFrame() {
        super("Test window");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        Container container = getContentPane();
        container.setLayout(new GridBagLayout());
        container.setBackground(new Color(240,240,240));
        JTextField editableTextField;

        editableTextField = new JTextField("Enter text here");
        constraints = new GridBagConstraints();
        constraints.fill=GridBagConstraints.BOTH;
        constraints.gridheight=2;
        constraints.gridx=0;
        constraints.gridy=0;
        container.add(editableTextField, constraints);

        pack();
    }

    public static void main(String args[]) {
        new TestFrame();
    }
}

I am using Eclipse 4.4.1 under MacOS 10.9.3.

Can anyone explain me, where the difference in view comes from and how can I get the proper text field height in jar?

Roman
  • 2,225
  • 5
  • 26
  • 55
  • `editableTextField.setPreferredSize(new Dimension(w,h));` – Edward J Beckett Dec 12 '15 at 10:00
  • @EddieB [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) – Yassin Hajaj Dec 12 '15 at 10:12
  • No... `setPreferredSize` is fine for this usecase. `setSize` is not acceptable. – Edward J Beckett Dec 12 '15 at 10:14
  • @EddieB Will it still work in a multicomponent window with different screen resolutions? – Roman Dec 12 '15 at 10:20
  • You're setting the `preferredSize` for the JTextField not a `fixedSize.` the layout manager (GridBag) can opt to ignore the size if it doesn't fit with other components so you need more context to determine that... `The FlowLayout and GridBagLayout managers use the component's preferred size (the latter depending on the constraints that you set), but BorderLayout and GridLayout usually do not.` @see [Layouts](https://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html) – Edward J Beckett Dec 12 '15 at 10:23
  • @YassinHajaj I'm confused... Are you stating to use setPreferredSize [or not](http://stackoverflow.com/questions/34160993/borderlayout-impossible-to-set-a-manual-location-swing-java/34161296#34161296) – Edward J Beckett Dec 12 '15 at 11:00

1 Answers1

1

how can I get the proper text field height in

You don't do anything. The Swing layout managers will determine the proper height for the text field based on the font of the text field.

In your case you only have a single component added to the frame so you can't tell the text field to have a gridHeight of 2 because there is only one row. That constraint is used when you have multiple rows of components in the frame. Then you can specify that a component spans two rows.

If you want extra space around the text field than you can change the Border:

JTextField textField = new JTextField(30);
Border empty = new EmptyBorder(10, 10, 10, 10);
Border compound = new CompoundBorder(textField.getBorder(), empty);
textField.setBorder(compound);

Read the section from the Swing tutorial on How to Use Borders for more information and examples.

The tutorial also has a section on How to Use GridBagLayout you should check out for more information about GridBagConstraints.

camickr
  • 321,443
  • 19
  • 166
  • 288