2

I am using the following code to create a very simple JFrame, but for some reason it doesn't show any components, just a blank frame. Why is this happening? I created frames a bunch of times and I just can't figure out what is wrong. The code is:

Main(){
    JFrame frame = new JFrame("Colorizer | By: NonameSL");
    frame.setSize(400,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    frame.setContentPane(panel);
    textField=new JTextField("Enter your name!");
    textField.setBounds(0,0,40,200);
    textField.setVisible(true);
    frame.getContentPane().add(textField);
    button=new JButton("Go!");
    button.setBounds(0, 200, 40, 200);
    button.setVisible(true);
    frame.getContentPane().add(button);
    rectangle=new RecShape(Color.WHITE);
    rectangle.setBounds(0,40,400,160);
    rectangle.setVisible(false);
    frame.getContentPane().add(rectangle);
    Main.frame=frame;
    registerButton();
}

The RecShape class is a class I created to simply create a rectangle shape on screen. What is wrong?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NonameSL
  • 37
  • 4
  • See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jun 27 '14 at 09:24
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 27 '14 at 09:30
  • Btw, never call `setBounds/setSize/setLocation` Leave all that to LayoutManager's, its their job, not yours. – Guillaume Polet Jun 27 '14 at 10:09

4 Answers4

3

Put frame.setVisible(true); after adding components to JFrame, and it will show all the added components. Moreover, you should use specific layout rather than setting bounds for components. You can use a Layout Manager.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Thanks a bunch! I tried to figure out what's wrong with my code for hours, I guess I didn't notice that. – NonameSL Jun 28 '14 at 08:52
2

You have to move frame.setVisible(true); to the end of the method; the visibility must be set to true after you have added the components.

Alternatively, you can add the following to the end of your method:

frame.revalidate();
frame.repaint();

to revalidate and repaint the frame with the newly added components although I recommend the former method.

David Yee
  • 3,515
  • 25
  • 45
  • Though the line `frame.revalidate()` will work on `1.7+`, in the previous versions, you have to use `frame.getContentPane().validate()` I guess – nIcE cOw Jun 27 '14 at 10:53
1

you can add this at the end;

frame.pack()
Arno_Geismar
  • 2,296
  • 1
  • 15
  • 29
1

Better your first add components to your variable "panel" and add then your finished panel to the .getContentPane().add().

And the most important issue is that you better call frame.setVisible(true); at the end of your method.

Rubinum
  • 547
  • 3
  • 18
  • 1
    "As a convenience `add` and its variants, `remove` and `setLayout` have been overridden to forward to the `contentPane` as necessary."—[`JFrame`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html) – trashgod Jun 27 '14 at 09:29
  • 1
    Correct; the original formulation is still seen in some tutorials. – trashgod Jun 27 '14 at 09:34