We help IT Professionals succeed at work.

JList question again

tiger0516
tiger0516 asked
on
Medium Priority
283 Views
Last Modified: 2008-03-03
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.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
                    }
                   
               }
          }
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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());
                    }
                   
               }
          }

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

Ask the Experts

Author

Commented:
I am unable to change the Event class, or the Image class. I am trying your 2nd comment.

Author

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)

Author

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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
What type of Objects are in your list
What does the Event class look like?

Author

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

Commented:
>>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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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());

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

Commented:
:-)
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.