Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting events from JList

 
Sophia Dever
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all: welcome to the JavaRanch.

Is test implementing the ListSelectionListener interface?
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is better suited for the UI forums.
Moving.
 
Sophia Dever
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clue: Think what this line means. What actually is "this"?
 
Sophia Dever
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic