• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSlider ChangeListener

 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been using Head First Java and I haven't come across how to set up a JSlider, so I tried treating it like JButton. This is the best I can do (in the code below), but if someone can tell me what I'm doing wrong, I'd appreciate it. Thanks.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with classes called BlueChangeListener and RedChangeListener. That should be ColourChangeListener (spelt differently in America) and the slider, colour, and output components should be passed as constructor arguments.
You have got the wrong name for the method in ChangeListener; the underlined text on the left will show you the correct method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and I think this thread would sit better on our GUIs forum, so I shall move it.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time please PostRealCode. Classes that extend ChangeListener instead of implementing it, then implementing the wrong method - no way that code was ever going to compile.

And a +1 on using one single class with the controls as fields.
And a +1 on using Colour over Color
 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the real code I wrote.

Like I said, the guide I've been using didn't have any examples on using JSlider, so I took what it did have on JButtons and tried applying it to JSlider. I've only been at Java for a few weeks.

I don't understand whate you mean by
Classes that extend ChangeListener instead of implementing it, then implementing the wrong method...
Which classes? Could you give me an example of how it should look and why it is supposed to be written that way, please.

Thanks.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go
http://download.oracle.com/javase/tutorial/uiswing/components/slider.html
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Graham wrote:I don't understand whate you mean by
Classes that extend ChangeListener instead of implementing it, then implementing the wrong method...
Which classes? Could you give me an example of how it should look and why it is supposed to be written that way, please.

 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing "extends" to "implements" in class redSliderListener yielded this result:

ColorPalette.redSliderListener is not abstract and does not override abstract method stateChanged(javax.swing.event.ChangeEvent) in javax.swing.event.ChangeListener

The same obviously with when I did the same for class greenSliderListener and class blueSliderListener. Any ideas?

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have to implement that method.

Go to everywhere where you are extending a superclass or implementing an interface, and put the override annotation @Override (copy-and-paste it) before the heading of every method. It is intended to pick up spelling errors. If you make a tiny mistake, eg writing StateChanged, you will get a method which is never invoked, and won't override the method you think you are overriding.
It only works for interfaces in Java™6, so for "override" read "implement".
 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and your spelling errors should produce a compiler error like "method foo does not override a superclass method."
 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I never switched "actionPerformed" to "stateChanged", so that was my last problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic