I am using GridBagLayout to produce my window application in Eclipse. I am getting a nice window in the Desing tab, like this:
However, when I export it into a jar and launch it, I get this: 
My code:
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);
        pack();
        setVisible(true);
        Container container = getContentPane();
        container.setLayout(new GridBagLayout());
        constraints = new GridBagConstraints();
        text0 = new JTextField("Some text", 20);
        constraints.fill=GridBagConstraints.HORIZONTAL;
        constraints.gridx=0;
        constraints.gridy=0;
        container.add(text0, constraints);
    }
    public static void main(String args[]) {
        TestFrame test = new TestFrame();
    }
}
Therefore, I always need to stretch the window manually to get the right size. Alternatively, I can use setSize() or setMinimumSize() in combination with pack() to achieve this.
The problem, however, is that setSize() and setMinimumSize() accept dimensions in pixels, which leads to two inconveniences:
- The new optimal size must be guessed by trial and error after each change in components number or size.
- The window size must be fit to system display settings.
Thus, the question is: is there a way to automatically calculate the optimal window size on the basis of used components in a jar (like it is done in the preview)?
SOLVED:
As pointed out by @trashgod, it was enough to move pack() to the end.


 
     
     
    