JList question again

I have a JList:
                                JList event;
            this.event=new JList(eventName); (eventName is an array that holds event's name)
            this.event.addListSelectionListener(new EventListener());

I also have a ListSelectionListener:

 class EventListener implements ListSelectionListener{
                  
                  public void valueChanged(ListSelectionEvent e)
                  {
                      JList list = (JList) e.getSource();
                        if (!e.getValueIsAdjusting())
                        {
                              //System.out.println(list.getSelectedValue());
                        }
                        
                  }
            }

What I want to do is: when users choose one list item, the other pane of the same window will display images related to this event.

For example, event list is:

Out for dinner
Busy for work

When I click "out for dinner", the other pane of the same frame will display images whose events are "out for dinner"

For Event class, I have a method to return all related images of a given Event. The Event class is in another package, and the Event constructor is not public (then it is package level, isn't it?

Thanks.
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:
You listener will need access to the other pane to change its details
One way to achieve this would be to make the other pane the listener

class Event extends Whatever implements ListSelectionListener{
               
               .....

               public void valueChanged(ListSelectionEvent e)
               {
                   JList list = (JList) e.getSource();
                    if (!e.getValueIsAdjusting())
                    {
                         //System.out.println(list.getSelectedValue());
                         // update the images now
                    }
                   
               }
          }
0
objectsCommented:
alternatively pass whatever is needed by the listener  and staore reference as member var


class EventListener implements ListSelectionListener{

       private Event mEvent = null;
       
       public EventListener(Event event) {
          mEvent = event;
       }
   
               public void valueChanged(ListSelectionEvent e)
               {
                   JList list = (JList) e.getSource();
                    if (!e.getValueIsAdjusting())
                    {
                         //System.out.println(list.getSelectedValue());
                    }
                   
               }
          }
0
tiger0516Author Commented:
I am unable to change the Event class, or the Image class. I am trying your 2nd comment.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

tiger0516Author Commented:
Within the EventListener, I tried this:

JList list = (JList) e.getSource();
Event e1=(Event)list.getSelectedValue();

Why it does not work? I though getSelectedValue() gets an Object; by casting, I would get an Event. But I am wrong.

I tried to use :

Event mEvent= new Event(list.getSelectedValue().toString()), does not work; Eclipse tells me the constructor Event(String) is invisible (Event's constructor does not have "public" keyword)
0
tiger0516Author Commented:
Follow-up

>Within the EventListener, I tried this:

>JList list = (JList) e.getSource();
>Event e1=(Event)list.getSelectedValue();

>Why it does not work? I though getSelectedValue() gets an Object; by casting, I would get an Event. But I am wrong.

I know why it is wrong now.
0
objectsCommented:
What type of Objects are in your list
What does the Event class look like?
0
tiger0516Author Commented:
>What type of Objects are in your list

In which list? event's JList is a list of all Event's name

The Event class is here: http://www.ugrad.cs.ubc.ca/~cs211/assignments/resources/javadoc/library/Event.html
0
CEHJCommented:
>>JList is a list of all Event's name

That would imply List<String>. To use your code, you'd need List<Event>, so  create a ListModel and add Event to it
0
objectsCommented:
If the list contains a list of Events then you can't cast it to Event (because it isn't one) but I think u figured that out alreayd. You can only cast it to the class (or superclass) of the Objects in the List. In your case a String.
You can then use that String to determine matching Event and get the photographs to display.
Another option would be to use an array of Events with your JList and a custom renderer to display the detail for each event as required.
Then you could cast the selected value to a Event (because it is one).
Your renderer would look like:

public class EventRenderer extends DefaultListCellRenderer
{
   public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
      Event event = (Event) value;
      return super.getListCellRenderer(list, event.getName(), index, isSelected, cellHasFocus);
   }
}

you'd then use it as:

 Event[] events = { event1, event2, event3};
 JList eventList = new JList(events);
 list.setCellRenderer(new EventRenderer());

0
CEHJCommented:
You're unlikely to need a custom renderer, so try it without one first. This is all you need (where 'events' here is an array of Event)


DefaultListModel dlm = new DefaultListModel();
for(int i = 0;i < events.length;i++) {
      dlm.add(events[i]);
}
listOfEvents = new JList(dlm);
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
CEHJCommented:
:-)
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.