简体   繁体   中英

Netbeans: JCombobox in Jframe with subindex

My English is not good, but I try.

I created 4 JComboBox in the NetBeans JForm

Combo1
Combo2
Combo3
Combo4

How do I call them by the number? For example:

i = 2;

String item = (String) combo(i).getSelectedItem();

This obviously does not work, I know.

And I can not create another array called combo[] because NetBeans considers it to be another JComboBox .

Is there any way to do it?

Or this can not be done in NetBeans?

And I can not create another array called combo[]

Sure you can. The array variable name would be "combo" and the individual combo boxes are "combo1", "combo2" etc.

The basic code is:

JCombobox[] comboBoxes = new JComboBox[4];
JComboBox combo1 = new JComboBox(...);
comboBoxes[0] = combo1;
JComboBox combo2 = new JComboBox(...);
comboBoxes[1] = combo2;

Then when you want to access the combo box you use:

String item = comboBoxes[i].getSelectedItem().toString();

How you actually create the combo boxes and add the to the frame is up to you, but then is no reason you can't add the combo box to an array.

because NetBeans

Don't use NetBeans to create the GUI. If you do you are spending time learning the IDE and the code won't be portable if you ever move to another IDE.

Instead, create the GUI manually and just use the IDE to compile and debug your code. This way you spend time learning Java, not the IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM