Aleks
Aleks

Reputation: 74

Java JCombobox causes rendering problems

Hello, I have the following problem:

public class TestCombo extends JFrame{

    public TestCombo() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,200);
        setVisible(true);

        setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,4));

        JLabel l1 = new JLabel("test1");
        JLabel l2 = new JLabel("test2");

        panel.add(l1);
        panel.add(l2);

//      JComboBox<String> combo = new JComboBox<String>();// <-- uncomment this for the problem

        this.add(panel, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        new TestCombo();
    }

}

As you can see, I am doing a very simple example. If I uncomment the marked part, the label items are not shown. If I resize the window, they are visible again. The strange thing here is that I do not even add the combo to the panel or anywhere. I am just instantiating it. Can someone tell me why I need to resize the frame to see the labels? Am I doing something wrong?

Upvotes: 0

Views: 1199

Answers (2)

gprathour
gprathour

Reputation: 15333

you are doing setVisible(true) in the very beginning.

You should do it after adding all components.

Upvotes: 5

Harry Joy
Harry Joy

Reputation: 59660

I don't think this is valid syntax for JComboBox

JComboBox<String> combo = new JComboBox<String>();

it should be

JComboBox combo = new JComboBox();

Also setVisible(true); should be after this.add(panel, BorderLayout.NORTH);.


How to use comboBox

Upvotes: 0

Related Questions