aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JSlider ChangeListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider ChangeListener" Watch "JSlider ChangeListener" New topic
Author

JSlider ChangeListener

Jeremy Graham
Greenhorn

Joined: Feb 14, 2011
Posts: 14
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
. . . and I think this thread would sit better on our GUIs forum, so I shall move it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeremy Graham
Greenhorn

Joined: Feb 14, 2011
Posts: 14
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

Joined: May 03, 2008
Posts: 4544
    
    5

Here you go
http://download.oracle.com/javase/tutorial/uiswing/components/slider.html


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Feb 14, 2011
Posts: 14
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
. . . and your spelling errors should produce a compiler error like "method foo does not override a superclass method."
Jeremy Graham
Greenhorn

Joined: Feb 14, 2011
Posts: 14
Thanks. I never switched "actionPerformed" to "stateChanged", so that was my last problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSlider ChangeListener