Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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);
            
      }
}
0
tiger0516
Asked:
tiger0516
  • 4
2 Solutions
 
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
0
 
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)
}
0
 
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);
                        }
0
 
CEHJCommented:
Make that

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

I prefer yours - didn't notice you were using >= 1.5
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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