Still a JList question

Hi folks,

I still cannot figure out how to do JList correctly. The following is my code.

Basically, there are three JList wrapped in one JPanel (will change to be wrapped in three JScrollPanes soon).

I want to have usess click an Event and then the imageList will display all images with this event, and similarily, have users click a Person and the imageList will display all images with this person. There are two methods in Event and Person classes, both with the name getPhotographs, which will return a collection of photographs according to the parameter (an Event or a Person).

Thanks a lot.

----------------------------------

public class JListPane extends JFrame{

      private Library library;
      private JList eventJList,personJList,imageJList;
      private JPanel test,eventPane,personPane,imagePane;
      public JListPane(Library l)
      {
            this.library=l;
            test=new JPanel();
            test.add(this.getEventJList());
            test.add(this.getImageJList());
            test.add(this.getPeopleJList());
            this.add(test);
      }
      
      public JList getEventJList(){
            DefaultListModel model=new DefaultListModel();
            model.addElement(library.getDefaultEvent());
            for (Event e:library.getEvents())
            {
                  model.addElement(e);
            }
            
            eventJList=new JList(model);
            //test.add(eventJList);
            this.eventJList.addListSelectionListener(new EventListener());
            return eventJList;
      }
      
      public JList getImageJList(){
            
            DefaultListModel model=new DefaultListModel();
            for (Photograph p:library.getPhotographs())
            {
                  model.addElement(p);
            }
            imageJList=new JList(model);
            //test.add(imageJList);
            this.imageJList.addListSelectionListener(new ImageListener());
            return imageJList;
      }
      
      public JList getPeopleJList(){
            DefaultListModel model=new DefaultListModel();
            for (Person p:library.getPersons())
            {
                  model.addElement(p);
            }
            this.personJList=new JList(model);
            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())
                  {
                        refreshImageListbyEvent(event);
                  }                  
            }
      }
   
    class PersonListener implements ListSelectionListener{
          
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList=(JList)e.getSource();
                  Person p=(Person)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        refreshImageListbyPerson(p);
                  }
            }
      }
   
    class ImageListener implements ListSelectionListener
    {
            
            public void valueChanged(ListSelectionEvent e)
            {
                  JList aList = (JList) e.getSource();
                  Photograph p=(Photograph)aList.getSelectedValue();
                  if (!e.getValueIsAdjusting())
                  {
                        System.out.println(p.getClass().toString());
                  }
            }
      }
   
   
    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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

CEHJCommented:
>>
public void refreshImageListbyEvent(Event e)
    {
         //To do
    }
>>

You need to get the images, build a ListModel (as I showed your earlier) and then set the model on the JList that displays each image
CEHJCommented:
public void refreshImageListbyEvent(Event e)
{
    Collection photos = e.getPhotographs();
    DefaultListModel dlm = new DefaultListModel();
    Iterator i = photos.iterator();
    while (i.hasNext()) {
        dlm.add(i.next());
    }
    theImageList.setModel(dlm); // (Or whatever the component's called)
}

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 also figure out one way:

Firstly, I declare three instant private fields, modelEvent, modelPerson,modelImage,

then, when I click an Event, I first remove all elements from modelImage, then add qualifed images to the model

                        modelImage.removeAllElements();
                        for (Photograph p:event.getPhotographs())
                        {
                              modelImage.addElement(p);
                        }
CEHJCommented:
Make that

dlm.addElement(i.next());
CEHJCommented:
:-)

I prefer yours - didn't notice you were using >= 1.5
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.