• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

getting events from JList

 
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.)
 
Bartender
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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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!
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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!
 
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
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!