-
Question about JLists
I have 2 JLists in the same GUI with 3 ComboBoxes and a bunch of Radio Buttons. I have the radio buttons grouped together.
In the GUI, when I selected an item from a ComboBox or RadioButton (which highlights it) and then select another component, the ComboBox/RadioButton item becomes unselected.
Whenever I choose an item from a JList and then click on a radio button, combo box, or another list... the item remains highlighted. Is there a way to have the selected item in the list become unselected (unhighlighted) when I click on another component?
-
Sorry, forgot to mention, I have tried using list.clearSelection(). But whenever I do this, the first list i click on gets selected normally. When I choose an item from the opposite list, it just outlines the selection (doesn't highlight it). If I click a 2nd time (on any item from the list) it becomes highlighted like normal. Then the problem just reverses and I must click on the other list twice to get an item to work.
This does not occur when I don't use clearSelection(), but the item from the opposing list remains highlighted at all times.
-
one way might be to add a class field
int selectedIndex = -1;
then add a focusListener to the JList (list)
Code:
list.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent fe){
list.setSelectedIndex(selectedIndex);
}
public void focusLost(FocusEvent fe){
selectedIndex = list.getSelectedIndex();
list.clearSelection();
}
})
-
Thanks, I got it to work, but I had to use...
Code:
list.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent fe){
list.setSelectedIndex(list.getSelectedIndex());
}
public void focusLost(FocusEvent fe){
list.clearSelection();
}
});
The way you had it was fine except that it selected the last known index when the list became focused instead of the item clicked.
THANKS!
-
I'd leave it selected, but perhaps that's just me.