I have Created a JComboBox
and its item are being added dynamically through a LinkedList
, how to initialize its selected value.
Suppose "list"
contains A->B->C->D->null
I want to intialize the ComboBox selected index with B (i.e 2nd Item In the list).
I have tried to do it as below
ComboBox.setSelectedIndex(1);
but I am getting Exception setSelectedIndex: 1 out of bound
JComboBox ComboBox= new JComboBoX();
LinkedList List = new LinkedList();
getListDataFromDataBase();
//After this List Contains A->B->C->D->null
for(int i=1;i<=List.getSize();i++)
{
Object Item = List.getValueAt(i);
ComboBox.addItem(Item);
}
ComboBox.setSelectedIndex(1);
Make sure elements are added into the
comboBox
, usingaddItem()
.Here's a small snippet:
EDIT
Adding from a
linkedList