Reputation: 3742
I observe the following behavior (on the Windows 7 platform) :
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame{
JPanel p;
JComboBox<String> l;
JLabel title;
public static void main(String[] arg){
Main m = new Main();
m.setVisible(true);
m.setSize(400,400);
m.p = new JPanel();
//m.l = new JComboBox<String>();
m.title = new JLabel("HELLO");
m.p.add(m.title);
m.setContentPane(m.p);
}
}
Displays HELLO
, but if I uncomment the line that instantiates the JComboBox
, it won't display anything. What could cause that? Are you able to reproduce the bug?
Upvotes: 0
Views: 325
Reputation: 59650
Solution from my comment:
Move
m.setVisible(true);
at the end.
Another comment from Jens Schauder:
Your code should also run in the EDT. Anything else is asking for trouble
May be he wants to tell something like this:
Everything dealing with Swing components, including there construction must run in the EDT. If it doesn't it is broken, although you might not notice it.
For that you may move your logic from main method to constructor of class and call constructor as follows:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
You can also write logic in some other method then constructor.
Upvotes: 2