Search code examples
javaswingresizejframejtextfield

JTextField resizing upon minimize


I have a JTextField as well as a JTextArea in a JFrame. When the application runs however, the JTextField gets resized if I minimize the window. It usually doubles or triples in height, but it's not consistent in how it resizes each time. I have no idea why this could be happening. I'm not really asking for a direct solution, just some possible things that could be causing the problem. If you could help me out here that'd be great. Thanks

EDIT: here's the code I'm using to initialize it:

public class Console extends JPanel implements ActionListener, Serializable {
    boolean ready = false;
    protected JTextField textField;
    protected JTextArea textArea;
    private final static String newline = "\n";
    Game m_game;
    Thread t;

public Console(Game game) {
    super(new GridBagLayout());

    m_game = game;
    textField = new JTextField(20);
    textField.setPreferredSize(null);
    textField.addActionListener(this);

    textArea = new JTextArea(20, 60);
    textArea.setEditable(false);
    textArea.setWrapStyleWord(true);
    textArea.setLineWrap(true);
    Font comic = new Font("Serif", Font.PLAIN, 14);
    textArea.setFont(comic);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Add Components to this panel.
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
    add(textField, c);
}

public void actionPerformed(ActionEvent evt) {
    String text = textField.getText();
    m_game.input = text;
    textField.selectAll();
    textArea.setCaretPosition(textArea.getDocument().getLength());
    m_game.wait = false;
    synchronized (m_game){
        ready = true;
        m_game.notifyAll();
    }
}

public void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add contents to the window.
    frame.add(new Console(m_game));

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

Solution

  • I think it's connected with Layout Manager. http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html