Note that there are some explanatory texts on larger screens.

plurals
  1. POJList clearSelection() issue
    text
    copied!<p>I have a problem with my two JList components. </p> <p>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)</p> <pre><code> 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); } } </code></pre> <p>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.</p> <p>I find this behaviour wierd and I don't want it to behave like this. Any suggestion?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload