Deselect a JList item

I have two JLists and add two different ListSelectionListeners to them.

Currently, when I run the problem, the default interface is no item chosen and two JList both show all items in list; when I click an item from one JList, the other JList will display related items.

Now, I want to have a reset fuction. Right now, if I select an item, for example, an Event from the eventJList, related Photos will display; however, if I deslect that (by using ctrl-left click), there are many errors.

How should I do can I deslect an item? For example, after clicking "lunch", I have all images about lunch; I want to deslect "lunch" and then get all images (lunch & dinner).

I tried clearSelection, does not work.

code below

public class JListPane extends JFrame{

      private Library library;
      private JList eventJList,personJList,imageJList;
      private JPanel test,desc,eventPane,personPane,imagePane;
      //private JScrollPane test,desc,eventPane,personPane,imagePane;
      DefaultListModel modelEvent,modelPerson,modelImage;
      public JListPane(Library l)
      {
            this.library=l;
            desc=new JPanel();
            modelEvent=new DefaultListModel();;
            modelPerson=new DefaultListModel();
            modelImage=new DefaultListModel();
            test=new JPanel();
            JButton initial=new JButton("Reset");
            initial.addActionListener(new buttonListener());
            test.add(this.getEventJList());
            test.add(this.getImageJList());
            test.add(this.getPeopleJList());
            test.add(initial,BorderLayout.SOUTH);
            test.add(desc);
            this.add(test);
      }
      
      public void initialize()
      {
            eventJList.clearSelection();
            imageJList.clearSelection();
            personJList.clearSelection();
      }
      
      public JList getEventJList(){
            modelEvent.addElement(library.getDefaultEvent());
            for (Event e:library.getEvents())
            {
                  modelEvent.addElement(e);
            }
            
            eventJList=new JList(modelEvent);
            //test.add(eventJList);
            this.eventJList.addListSelectionListener(new EventListener());
            return eventJList;
      }
      
      public JList getImageJList(){
            
            DefaultListModel tempModel=new DefaultListModel();
            
            for (Photograph p:library.getPhotographs())
            {
                  modelImage.addElement(p);
                  tempModel.addElement(p.getLocation());
            }
            imageJList=new JList(modelImage);
            //test.add(imageJList);
            this.imageJList.addListSelectionListener(new ImageListener());
            return imageJList;
      }
      
      public JList getPeopleJList(){
            for (Person p:library.getPersons())
            {
                  modelPerson.addElement(p);
            }
            this.personJList=new JList(modelPerson);
            test.add(personJList);
            this.personJList.addListSelectionListener(new PersonListener());
            return personJList;
      }
      
      class EventListener implements ListSelectionListener{
            
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList=(JList)e.getSource();
                  Event event=(Event)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        modelImage.removeAllElements();
                        for (Photograph p:event.getPhotographs())
                        {
                              modelImage.addElement(p);
                        }
                  }                  
            }
      }
   
    class PersonListener implements ListSelectionListener{
          
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList=(JList)e.getSource();
                  Person p=(Person)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        eventJList.grabFocus();
                        modelImage.removeAllElements();      
                        for (Photograph p1:p.getPhotographs())
                              {
                                    modelImage.addElement(p1);
                              }
                  }
            }
      }
   
    class ImageListener implements ListSelectionListener
    {
            
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList = (JList) e.getSource();
                  Photograph p=(Photograph)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        
                  }
            }
      }
   
    class buttonListener implements ActionListener
    {
          public void actionPerformed(ActionEvent e)
          {
                initialize();
          }
    }
   
    public void refreshImageListbyEvent(Event e)
    {
          //To do
    }
   
    public void refreshImageListbyPerson(Person p)
    {
          //To do
    }
   
    public JFrame getLargeWindow(Photograph p)
    {
          JFrame largeWindow=new JFrame();
          //to-do
          return largeWindow;
          
    }
   
    public static void main(String[] args) {            
            
            File libraryFile = new File("PhotoAlbum.xml");
            Library library = Library.getInstance();
            if (libraryFile.exists()) {
                  try {
                        LibraryStore.loadLibrary(library, libraryFile);
                  } catch (IOException e1) {
                        e1.printStackTrace();
                  } catch (ParserException e1) {
                        e1.printStackTrace();
                  }
            }
            if (args.length > 0 && args[0] != "") {
                  for (String dir : args[0].split(File.pathSeparator)) {
                        JPEGTagReader.loadDirectory(library, dir);
                  }
            }
            
            JListPane frame=new JListPane(library);
            frame.setSize(800,800);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
            
      }
}

LVL 1
tiger0516Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
Your listener should be checking if the selected value is null or not
IF it is then display all images
if not null then display images for that event
0
tiger0516Author Commented:
>IF it is then display all images
>if not null then display images for that event

How? I have a modelImage, modelEvent, modelPreson (DefaultListModel) which are used to hold related objects

For example, the event listener is:

      class EventListener implements ListSelectionListener{
            
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList=(JList)e.getSource();
                  Event event=(Event)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        modelImage.removeAllElements();
                        for (Photograph p:event.getPhotographs())
                        {
                              modelImage.addElement(p);
                        }
                  }                  
            }
      }

When clicked, I first remove all elements from modelImage and then add qualifed elements to modelImage again.

What should I do to deselect an item and show all images again?

Thanks
0
objectsCommented:
you'd use something like:

                    if (event==null)
                    {
                         // nothing selected, display all
                         ListModel model = aList.getModel();
                         for (int i=0; i<model.getSize(); i++)
                         {
                              Event e = (Event) model.getElementAt(i);
                              for (Photograph p:e.getPhotographs())
                              {
                                  modelImage.addElement(p);
                              }
                         }
                    }
                    else
                    {
                        for (Photograph p:event.getPhotographs())
                        {
                           modelImage.addElement(p);
                        }
                    }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tiger0516Author Commented:
Thanks.

I just add one line below:

modelImage.removeAllElements();
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.