3

I am using GridBagLayout to produce my window application in Eclipse. I am getting a nice window in the Desing tab, like this:

enter image description here

However, when I export it into a jar and launch it, I get this:

enter image description here

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:

  1. The new optimal size must be guessed by trial and error after each change in components number or size.
  2. 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.

Roman
  • 2,225
  • 5
  • 26
  • 55
  • 1
    *"Optimal window size with Java Swing"* `pack()` (after adding components with suitable white space). See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 14 '15 at 11:10
  • @AndrewThompson I use `pack()`; however, it does not set window size in a `jar` (I get 0x0 window, which I need to stretch). – Roman Nov 14 '15 at 11:19
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 14 '15 at 11:21
  • @AndrewThompson Done! – Roman Nov 14 '15 at 11:32
  • 1
    Your example would attempt to `pack()` an empty window if `main` weren't empty. – trashgod Nov 14 '15 at 11:39

2 Answers2

0

To Solve this go and find container.add(text0, constraints); below this line call pack(); Method which was define in JFrame class.

For more information visit.

Community
  • 1
  • 1
Sadhan Sarker
  • 129
  • 3
  • 11
0

What happens here is that you pack the JFrame before adding any components to it. The size of the JFrame that is needed is 0x0 because there are no components at this point. Adding components later does not change the size of the JFrame unless you set the size or call pack() again.

To fix this, add the pack(); to the end of the constructor. Ex:

public TestFrame() {
    super("Test window");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    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);
    pack();//Moved to last line
}
shan1024
  • 1,389
  • 7
  • 17