Get Selected Index

Asked

Viewed 38 times

0

Good afternoon. I want to set the option of a Jcombobox field as the registered option in a given vector position. For a previous text field, I made the following settings:

campoCpf.setText(vetAluno[i].getCpf());

To set the Jcombobox, as I would call the selected option of the array, assuming, for example, 3 options, if I selected one of them and the register was saved as that option chosen, how to call?

I know I must set the field like this:

campoCurso.setSelectedIndex(); 

but which parameter should I pass?

  • You have a variable and you want the JComboBox select the position that has the value of this variable, is this?

1 answer

0

Just pass the object you want to select within the method setSelectedItem.

Thus:

String [] s = {"orange", "pear", "apple", "banana"};

JComboBox test = new JComboBox(s);
test.setSelectedItem("banana");

The method setSelectedIndex, that you mentioned, select by the position in the vector. If we wanted to select banana using it, would look like this:

String [] s = {"orange", "pear", "apple", "banana"};

JComboBox test = new JComboBox(s);
test.setSelectedIndex(3);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.