Click to See Complete Forum and Search --> : JList returns null or -1


sportacus
12-07-2005, 08:27 AM
Hi Guys,

I have a JList that I want to double click and get the String that is currently selected.However, the .getSelectedValue.toString keeps returning null or -1 even when I have a String selected.

My code is as follows:


public void makeLstChatters(){
//make the listener for the JList
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
try{
System.out.println(lstChatters.getSelectedValue().toString());
String str = lstChatters.getSelectedValue().toString();
serv.whisper(str, txtAreaMessage.getText());
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
};

listModel = new DefaultListModel();

lstChatters = new JList(listModel);

JScrollPane scrollPane = new JScrollPane(lstChatters);
lstChatters.addMouseListener(mouseListener);


can anyone see where I am going wrong?

Thanks a million!!

Joe Beam
12-07-2005, 08:08 PM
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}

then to get the string clicked...

listModel.get(index)

sportacus
12-08-2005, 10:33 AM
excellent. thats worked a treat.

Thanks a million!!