Search code examples
javaswingjlistdefaultlistmodellistselectionlistener

Java - Clearing the selection in OTHER JList


I have the following problem: There are 2 JLists in my frame:

    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);

Now, these are mutually exclusive, so if someone clicks an item in the one list, the previous selection in the other list should be cleared.

  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);

On calling the Value is adjusting function I always get the nullpointerException:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Books$3.valueChanged(Books.java:199) at javax.swing.JList.fireSelectionValueChanged(Unknown Source)

Why is that? As you can see, they have been declared and should be accessible.


Solution

  • Thanks to Robin I figured it out. I wasn't aware that the method clearselection() triggers the listener. It's quick and dirty but it works:

        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 {
    
                      }
                  }
    
    
            }
            };
    

    Thanks again!