Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

JList question

Hi folks,

I have a JList

String[] name={......}
JList j=new JList(name)
How shall I do in the ListSelectionListener so that when I click one list item, Java will print that "xxx (the name of the selected item) is selected"

thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Easiest is to make you list a member variable and use:

public void       valueChanged(ListSelectionEvent e)
{
   System.out.println(j.getSelectedItem());
}
or o/wise:

public void       valueChanged(ListSelectionEvent e)
{
    JList list = (JList) e.getSource();
    System.out.println(list.getSelectedItem());
}
Avatar of tiger0516
tiger0516

ASKER

I use Eclipse. It says the method getSelectedItem()) is undifined for the type JList.

(I use your 2nd comment)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial