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

x
?
Solved

JList question

Posted on 2006-04-01
4
Medium Priority
?
258 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:tiger0516
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16352654
Easiest is to make you list a member variable and use:

public void       valueChanged(ListSelectionEvent e)
{
   System.out.println(j.getSelectedItem());
}
0
 
LVL 92

Expert Comment

by:objects
ID: 16352658
or o/wise:

public void       valueChanged(ListSelectionEvent e)
{
    JList list = (JList) e.getSource();
    System.out.println(list.getSelectedItem());
}
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16352670
I use Eclipse. It says the method getSelectedItem()) is undifined for the type JList.

(I use your 2nd comment)
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 16352696
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

577 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