views:

23

answers:

2

I have a JList with 5 options in it and when one of the items becomes selected or clicked i want the text area next to it to show a paragraph of text relative to the item clicked. It should do this for each item in the list but I cant seem to find how to do it in the API

How would my program know if an item in the JList was selected so I can work with the data?

+1  A: 

You should register a Listener for events on your JList. When the Swing UI fires one off, this Listener class will get the message and react accordingly.

duffymo
+4  A: 

Use addListSelectionListener. You can create a subclass (anonymous or not) of ListSelectionListener that does the work you want.

myList.addListSelectionListener(new ListSelectionListener()
{
  public void valueChanged(ListSelectionEvent ev)
  {
    // handle ev
  } 
});
Matthew Flaschen
Would using getSource() retrieve the value in a string of the selected item? That is what I want. To retrieve the item in a string of the actual selected item.
Kitsune
@Kitsune, the source is the JList. You can easily get the selected items with `((JList)ev.getSource()).getSelectedValues()`
Matthew Flaschen
Thanks alot! youve helped me so much! Youve been added as the answer and got an up! :P
Kitsune