Problem with adding text to a ComboBox


I´m not sure if i ask in the right forum but i´ll just have a try (please tell me if i should ask somewhere else):
I attempt to create my first GUI in JAVA. A friend told me that i should give IDEA a try.
I created a very simple GUI form with IDEA´s GUI-Designer.
Now i have a problem (i´am sure you will laugh) with adding text to a ComboBox. I´__ve posted the important part of the code (you´ll laugh again) to explain the problem. All i want is that "1999" and "2000" appear in the ComboBox. But after running the Prog the ComboBox has no content at all.
Simple question for you: Why??

Please help!

Björn vorm Walde

Comment actions Permalink

public class firstTry {

JPanel mainPanel;
JComboBox myCombo;
JButton myButton;

String[] years = {"1999", "2000"};

public firstTry() {

myCombo = new JComboBox(years);

The problem lies here. If you use UI builder, IDEA will create the UI component for u. and if you re-assign it, it is not added to the content pane, and you created a jcombobox, but will never be seen. In this case, you should use myCombo.addItem(...) to do that. In other cases, maybe you should construct some data model and pass to the UI. When you first try UI Builder, you can set the option to let IDEA generates source code and browse through it. That would help.


public static void main (String[] args) {

firstTry form = new firstTry();

final JFrame frame = new JFrame("Test");





Comment actions Permalink

Thank you very much tcmaster!

OK, so my new CombBox wasn´t added to my mainPanel and setContentPane(mainPanel) could´t get it. That´s why i have to put my text into the "original" ComboBox with addItem(""). Easy because well explained..... :)

Thanks again,


Please sign in to leave a comment.