• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2716
  • Last Modified:

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

0
tiger0516
Asked:
tiger0516
  • 2
  • 2
1 Solution
 
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
 
tiger0516Author Commented:
Thanks.

I just add one line below:

modelImage.removeAllElements();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now