Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of the item selected from a JComboBox?

This is how I created my JComboBox -

String[] options = {"First", "Second" , "Third"};
JComboBox optionsCombo = new JComboBox(options);

When one of these items is selected, how do i get the index of the item which was selected ? I don't want the item the item that was selected.

like image 919
SuperStar Avatar asked Nov 30 '22 13:11

SuperStar


2 Answers

int index = optionsCombo.getSelectedIndex() 

will give selected index. Use this in combo box action listener

like image 185
Mohan Raj B Avatar answered Dec 04 '22 15:12

Mohan Raj B


indexes starts from 0,1,2,.. if you want to get the index of selected item then do this

optionsCombo.getSelectedIndex()
like image 34
Arun Kumar Mudraboyina Avatar answered Dec 04 '22 16:12

Arun Kumar Mudraboyina