JList setSelectedValue not working

539 views Asked by At

I have a JList in Swing working bad. I list all items from Database into the list with no problem with this code.

My code:

Integer index = null;

DefaultListModel<String> model = new DefaultListModel<String>();
index = DataBase.getIndex1(cbActivity.getSelectedItem().toString());
activities = DataBase.getIndex2(index); 

for(MapActivity mapActitivy : activities)
{
   model.addElement(mapActivity.getActivity().toString());
}    
jList.setModel(model); 

But now, I would like to select individual or multiple selection, but nothing I tried works. I tried:

jList.setSelectedValue("Ball", true);
//jList.setSelectedIndex(2);
jList.setSelectionBackground(Color.red);

But nothing happen. Just the list on screen with nothing selected. Single or multiple.

Any help?

1

There are 1 answers

0
nil4925 On

Try this:

setSelectedIndex(1); // here use index of items

or if it does not work use below one:

setSelectedItem("ball") // here use name of item.