开发者

JList clearSelection() issue

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 beha开发者_开发百科ve like this. Any suggestion?


Your problem is that your ListSelectionListener gets notified in response to the clearSelection() call from the other list. When you make a selection on jlist1, it calls clearSelection on jlist2; if jlist2 has anything selected, this will trigger valueChanged on jlist2's selection listener, clearing the selection you just finished making on jlist1. You'll probably need to add a flag that lets the two listeners know if the other one is currently changing:

//member variable
boolean isChanging = false;

//later.... make this same change in both ListSelectionListeners!
jList2.addListSelectionListener(new ListSelectionListener() {

 public void valueChanged(ListSelectionEvent e) {
      if (!isChanging) {
        isChanging = true;
        jList1.clearSelection();
        isChanging = false;
      }
 }

});


you can access the event variabl e, which can indicate if this current event is a series of multiple events getValueIsAdjusting()

public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting())
       jList1.clearSelection();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜