Java-清除OTHER JList中的选择



我有以下问题:我的框架中有两个JList:

    listModel = new DefaultListModel();
    ownlistModel = new DefaultListModel();
    fillList();

    ownBookList = new JList(ownlistModel);
    ownBookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ownBookList.setLayoutOrientation(JList.VERTICAL);
    ownBookList.setSelectedIndex(-1);

    userlist = new JList(listModel);
    userlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    userlist.setLayoutOrientation(JList.VERTICAL);
    userlist.setSelectedIndex(0);

现在,这些都是互斥的,所以如果有人单击了一个列表中的某个项目,则应该清除另一个列表的上一个选择。

  ls2Handler = new ListSelectionListener(){
            @Override
                public void valueChanged(ListSelectionEvent e) {
              if (e.getValueIsAdjusting() == false) {

            //  System.out.println(ownBookList.getSelectedValue().toString().length());
                 Global.selectedUser =  ownBookList.getSelectedValue().toString();
                 Global.selectedIndex = ownBookList.getSelectedIndex();
             userlist.clearSelection();
                  updateFields(Global.selectedUser, 1);
              }

        }
    };
        lsHandler = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                  if (e.getValueIsAdjusting() == false) {
                //  System.out.println(userlist.getSelectedValue().toString().length());
                      Global.selectedUser =  userlist.getSelectedValue().toString();
                     Global.selectedIndex = userlist.getSelectedIndex();
                   ownBookList.clearSelection();
                      updateFields(Global.selectedUser, 0);
                  }

            }
            };

        userlist.addListSelectionListener(lsHandler);
        ownBookList.addListSelectionListener(ls2Handler);

在调用Value is adjusting函数时,我总是得到nullpointerException:

线程"AWT-EventQueue-0"java.lang.NullPointerException中出现异常在Books$3.valueChanged(Books.java:199)在javax.swing.JList.fireSelectionValueChanged(未知来源)

为什么?正如您所看到的,它们已经声明并且应该是可访问的。

getSelectedValue()方法可以返回null,并且您正在null对象上调用toString()

userlist.getSelectedValue().toString();

请参阅该方法的文档:

返回所选最小单元格索引的值;在列表中仅选择单个项目时的选定值。当选择多个项目时,它只是所选最小索引的值。如果没有选择,则返回null。

由于在列表A上选择某个内容时清除了列表B上的选择,您将很快遇到此异常。

多亏了Robin,我明白了。我不知道clearselection()方法触发了监听器。它又快又脏,但它能工作:

    ls2Handler = new ListSelectionListener(){
        @Override
            public void valueChanged(ListSelectionEvent e) {
          if (e.getValueIsAdjusting() == false) {
              if (ownBookList.getSelectedValue()!=null) {
                  userlist.clearSelection();
                  Global.selectedUser =  ownBookList.getSelectedValue().toString();
                  Global.selectedIndex = ownBookList.getSelectedIndex();
                  updateFields(Global.selectedUser);
              }
                else {
                    }
          }
    }
};
    lsHandler = new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
              if (e.getValueIsAdjusting() == false) {
                  if (userlist.getSelectedValue()!=null) {
                     ownBookList.clearSelection();
                      Global.selectedUser =  userlist.getSelectedValue().toString();
                      Global.selectedIndex = userlist.getSelectedIndex();
                      updateFields(Global.selectedUser);

                  }
                  else {
                  }
              }

        }
        };

再次感谢!

相关内容

  • 没有找到相关文章

最新更新