aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes getting events from JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getting events from JList" Watch "getting events from JList" New topic
Author

getting events from JList

Sophia Dever
Greenhorn

Joined: Sep 18, 2010
Posts: 4
I'm trying to register to get events from a JList The JList itself works fine; after defining "racelist", I say:



When I compile, it gives me the following error:

test.java:18: addListSelectionListener(javax.swing.event.ListSelectionListener) in javax.swing.JList cannot be applied to (test)
racelist.addListSelectionListener(this);
1 error


So is that not the right way? It's what Head First Java says to do.

(Edited because of confusion between JList and JScrollPane.)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all: welcome to the JavaRanch.

Is test implementing the ListSelectionListener interface?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Sophia Dever wrote:after defining "racelist"

Defining it as what? TellTheDetails

Your subject line appears to be misleading, as you cannot add a ListSelectionListener to a JScrolPane. To get better help sooner, post a SSCCE.

Also note that, by convention, class names should start with an uppercase letter. Test, not test.

And welcome to the Ranch!


luck, db
There are no new questions, but there may be new answers.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

This thread is better suited for the UI forums.
Moving.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Sophia Dever
Greenhorn

Joined: Sep 18, 2010
Posts: 4
You're right about the subject line. Sorry, I always get the JScrollPane and the JList confused.

Here is the full test program:



Obviously in a full program this would include adding the list to the frame and displaying, but this is the relevant code.

All I want is to be informed when someone clicks on one of the options.

Thanks for bearing with me!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sophia Dever wrote:.....
Here is the full test program:
.....

Your full test program should run and display a JFrame with the JList. And so it should have a main method and anything else absolutely necessary to display this minimal GUI.

Luck.
Sophia Dever
Greenhorn

Joined: Sep 18, 2010
Posts: 4
But the original program didn't have a main method--it was a class used in another program.

Should I put in a main method anyway? Or is it possible that that was affecting/causing the error?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sophia Dever wrote:But the original program didn't have a main method--it was a class used in another program.

Should I put in a main method anyway? Or is it possible that that was affecting/causing the error?


We don't want the original program, we want a test program that we can compile, run, test, and correct. Your code posted doesn't compile nor does it run. So yes, you'll need to add a main method, fix your code so that it compiles, and have it display a GUI that shows your problem. Please have a look at this link for more on the requirements of creating and posting a test program: http://mindprod.com/jgloss/sscce.html

Much luck!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

Clue: Think what this line means. What actually is "this"?
Sophia Dever
Greenhorn

Joined: Sep 18, 2010
Posts: 4
It's working!

After some more research, I realized I needed to import javax.swing.event.ListSelectionListener and javax.swing.event.ListSelectionEvent, and implement ListSelectionListener.

A final Thank You to everyone for their help--I hope to figure out some more about using Java forums over time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting events from JList