We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Still a JList question

tiger0516
tiger0516 asked
on
Medium Priority
495 Views
Last Modified: 2008-02-01
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);
            
      }
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
>>
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016
Commented:
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)
}

Author

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);
                        }
CERTIFIED EXPERT
Top Expert 2016

Commented:
Make that

dlm.addElement(i.next());
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

I prefer yours - didn't notice you were using >= 1.5
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.