• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSlider ChangeListener

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and I think this thread would sit better on our GUIs forum, so I shall move it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go
http://download.oracle.com/javase/tutorial/uiswing/components/slider.html
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I never switched "actionPerformed" to "stateChanged", so that was my last problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic