0

I am trying to make a box in Swing that has a label of "user", a text field for the username, and a button "sign in". This is my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Engine
{
    JFrame frame;
    public void go()
    {
        setUpGui();
        userNameScreen();
    }
    public void setUpGui()
    {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public void userNameScreen()
    {
        JPanel background = new JPanel();
            frame.getContentPane().add(background);
        JLabel labelUserName = new JLabel("User:");
            background.add(labelUserName);
            System.out.println(labelUserName.getHeight()); // 0
        JTextField textFieldUserName = new JTextField();
            System.out.println(labelUserName.getHeight()); // 16
            textFieldUserName.setPreferredSize(new Dimension(110,labelUserName.getHeight()));
            background.add(textFieldUserName);
        JButton buttonSignIn = new JButton("Sign In");
            background.add(buttonSignIn);
        /*
        background.add(labelUserName);
        background.add(textFieldUserName);
        background.add(buttonSignIn);
        frame.getContentPane().add(background);
        */
        frame.pack();
    }
}

My driver class just creates an instance of engine, then runs the method go().
I read that Swing components do not have attributes of height/width until they are added (because that is for the layout manager to decide how much room they have), so it makes sense that in the method userNameScreen(), adding in all components at the end* (commented out here) makes the textFieldUserName variable have no height.
However, you can see in that same method userNameScreen(), I have it do

System.out.println(labelUserName.getHeight());

twice. The first time, it is 0. The second, it is 16. I don't understand why the first time, it would register it as 0. It has already been added to the panel (in the line before), and there doesn't seem to be anything that would change its height between that first println() and the next. So why is the value 0 in the first one, and why does it change to 16 almost immediately after?

*I should note, when I say adding in all the stuff commented out at the end, it also includes removing/commenting out all the same commands done elsewhere in the code.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alex G
  • 747
  • 4
  • 15
  • 27
  • 1
    unrelated, but important: don't use setXXSize _ever_ For reasons, see http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519 – kleopatra Jun 27 '12 at 16:31

2 Answers2

2

It is a side effect from not creating/modifying your Swing components on the EDT. Now the EDT is busy doing the layout while you are adding components in another thread.

Your main method should look like:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Engine().go();
        }
    });
}
Walter Laan
  • 2,956
  • 17
  • 15
  • He hasn't created another thread, y r u saying that the EDT is busy doing the layout. EDT SHOULD be busy doing the layout. – Vedant Agarwala Jul 01 '12 at 06:14
  • @vedant1811 The main thread (created by the JVM) is not the EDT. The EDT gets created by Swing/AWT when you create a component automatically, so you end up with (at least) two thread. – Walter Laan Jul 02 '12 at 10:36
  • @walterLann i understand that now. So using inovkeLater causes everything to run on the EDT? – Vedant Agarwala Jul 02 '12 at 10:41
1

I'm not sure why this is happening but may be because the addition of the component maybe on a background thread and might not have been updated till the next statement is called and its updated a few millisecs later and appears when you call it second time.

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89