0

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?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Welcome to Stack Overflow, from your user name I think you're from latin america, am I right? If so, do you speak spanish? If writing your question in english is hard for you and you speak spanish there is [Stack Overflow en Español](https://es.stackoverflow.com/) or [Stack Overflow em Português](https://pt.stackoverflow.com/). However be sure that if you stay or move your question to those sites, to take the [tour], read [ask] and how to make (and post) a proper [mcve] that demonstrates your problem and try to explain your question as clearly as possible (you can even use Google Translate) – Frakcool Jul 03 '17 at 18:04
  • As for your question, this is not how Java works, try not to rely on GUI builders, you could either A) Create a `JComboBox` array and do it by hand or B) Call the `JComboBox` you want to access through its name directly, you can't concatenate the value of a variable inside the program... However you could C) Use an `ItemListener` or `ActionListener` as shown in [this question](https://stackoverflow.com/questions/58939/jcombobox-selection-change-listener) for example... – Frakcool Jul 03 '17 at 18:10

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you. But I already know how to create JComboBox arrays and know how to use and implement them. My question is if in Netbeans there is a way to do it when the JCombox is already made in the JForm. – Juan H. Jul 04 '17 at 16:59