Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JList question again

Posted on 2006-04-01
11
Medium Priority
?
268 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.
0
Comment
Question by:tiger0516
  • 4
  • 4
  • 3
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16353040
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
 
LVL 92

Assisted Solution

by:objects
objects earned 1200 total points
ID: 16353046
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
 
LVL 1

Author Comment

by:tiger0516
ID: 16353102
I am unable to change the Event class, or the Image class. I am trying your 2nd comment.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:tiger0516
ID: 16353111
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
 
LVL 1

Author Comment

by:tiger0516
ID: 16353120
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
 
LVL 92

Expert Comment

by:objects
ID: 16353121
What type of Objects are in your list
What does the Event class look like?
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16353190
>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16353307
>>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
 
LVL 92

Expert Comment

by:objects
ID: 16353638
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 16353675
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16356260
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question