3

Can someone explain why nothing is being displayed inside the application window when I run this?

It seems kind of weird, I had it displaying the JButtons just fine until I added a couple more, then it seemed to have affected everything.

Here is my code:

public class Window extends JFrame {

    JButton zero, one, two, three, four, five, six, seven, eight, nine;
    JButton add, sub, mul, div, mod;
    JPanel mainPanel;
    JLabel results;

    public Window(){
        this.setSize(400, 400);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Calculator");
        this.setVisible(true);

        mainPanel = new JPanel();

        results = new JLabel("RESULTS");
        results.setToolTipText("Calculated Results");

        zero = new JButton("0");
        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");

        add = new JButton("+");
        sub = new JButton("-");
        mul = new JButton("*");
        div = new JButton("/");
        mod = new JButton("%");

        this.add(mainPanel);

        mainPanel.add(zero);
        mainPanel.add(one);
        mainPanel.add(two);
        mainPanel.add(three);
        mainPanel.add(four);
        mainPanel.add(five);
        mainPanel.add(six);
        mainPanel.add(seven);
        mainPanel.add(eight);
        mainPanel.add(nine);

        mainPanel.add(add);
        mainPanel.add(sub);
        mainPanel.add(mul);
        mainPanel.add(div);
        mainPanel.add(mod);

        mainPanel.add(results);

    }

    public static void main(String[] args) {
        new Window();
    }
}
mubeen
  • 813
  • 2
  • 18
  • 39
TheDetailer
  • 289
  • 5
  • 16
  • 1
    I would try adding a layout to your mainPanel, and move `setVisible(true);` to the end of the constructor. – Clark Kent Nov 09 '15 at 16:25
  • Creating the GUI on the main thread is not recommend. Use SwingUtilities.invokeLater(); with a Runnable instance. – Doc Nov 09 '15 at 16:51
  • @shivam7357 This is [not entirely true](http://stackoverflow.com/questions/7156949/main-thread-vs-ui-thread-in-java). – Tomas Nov 09 '15 at 17:22

2 Answers2

2

When you call setVisible(true), your JFrame is rendered and displayed.

If you add stuff to it after that, those things will not be visible until you call validate(), pack() or setVisible(true) again.

  • validate() re-validates your component. It is "invalidated" whenever you add stuff to it, like in your code. It has to re-validated for those changes to show up.
  • pack() sizes your component (the JFrame), using any layout managers to calculate the sizes of it. It also re-validates it.
  • setVisible(true) does just what it sounds like, but since it was already visible the interesting bit for you is that it also re-validates the component.

So, what you should do after adding the components:

  1. Call setVisible(), or
  2. Call validate()

The second one only in case you actually want to dynamically add stuff to your panel, which I doubt in this case :)

Your code would look like this:

mainPanel.add(results);

pack();
setVisible(true);

And the earlier call to setVisible() (line 18) can be removed.

You might want to add a layout to your window too, and the Java™ Tutorial page on JFrames is probably useful.

Community
  • 1
  • 1
Tomas
  • 1,315
  • 10
  • 17
0

Edit your code in this way:

        ....
        mainPanel.add(results);
        this.setVisible(true);//Move this line to this place(at the end)
    }
    public static void main(String[] args) {
        new Window();
    }
}
Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Doc
  • 10,831
  • 3
  • 39
  • 63