hello, I have a problem with my two JList components.
I created and placed on JFrame two JList components. I added listSelectionListeners to both of them that should unselect a selection of the other JList like so: (edit: To make as requested self-contained sample)
public class JListIssue {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
frame.setContentPane(mainPanel);
final JList jList1 = new JList();
mainPanel.add(jList1);
final JList jList2 = new JList();
mainPanel.add(jList2);
// Setting up models for both JList components to display example values
jList1.setModel(new AbstractListModel() {
String[] items = {"Item 1", "Item 2", "Item 3"};
public int getSize() { return items.length; }
public Object getElementAt(int i) { return items[i]; }
});
jList2.setModel(new AbstractListModel() {
String[] items = {"Item 1", "Item 2", "Item 3"};
public int getSize() { return items.length; }
public Object getElementAt(int i) { return items[i]; }
});
// Adding listeners
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList2.clearSelection();
}
});
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1.clearSelection();
}
});
frame.pack();
frame.setVisible(true);
}
}
However, when it deselects the selection of one of those two lists I have to click two times to make a new selection. Because first time I click it sort of borders the item I want to select by it doesn't really selects it (confirmed by listSelectionListener) so I have to either move my mouse while holding the left mouse button during the first selection or click second time and then it actually selects the item.
I find this behaviour wierd and I don't want it to behave like this. Any suggestion?