I have been experimenting with Jsliders for awhile now, however every time I try to add ChangeListeners, I can't seem to get them to work, I have tried writing the code a couple of dozen different ways without success.
Here is my current source code: ________________________________________________
public void stateChanged(ChangeEvent evt) { JSlider source = (JSlider)evt.getSource(); if (source.getValueIsAdjusting() != true) { myJLabel.setText("Slider has been changed to: " + evt.getValue()); } }
public static void main(String[] arguments) { SliderTest frame = new SliderTest(); }
}
__________________________________________
This is the error I get when I try to compile:
SliderTest.java:31: cannot find symbol symbol : method getValue() location: class javax.swing.event.ChangeEvent myJLabel.setTest("Slider has been changed to: " + evt.getValue()); ^
All the Application is supposed to do is display the currect value selected on the slider, in the JLabel next to it.
Does anyone have any suggestions?
Thanks, Charles [ April 30, 2006: Message edited by: Charles Souders ]
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
The short answer is, that your event doesn't have a getValue() method. Your JSlider has, and you have called it source. Try that.
But why are you adding the ChangeListener to the Frame? Somebody asked about that recently onthis thread, and whoever responded (myself included) disliked the practice of writing myComponent.addSomethingListener(this); Add the ChangeListener to the JSlider, using exactly the same steps I described on that thread, but writing Change instead of Action, and using a different method. Much easier to handle. Lose the "implements ChangeListener" from your outer class. Don't write if(source.getValueIsAdjusting() != true);, write if(!source.getValueIsAdjusting());.
CR
Charles Souders
Greenhorn
Joined: Apr 25, 2006
Posts: 5
posted
0
Im new to this forum, so I have not seen your previous posts. I will try your advice on both the error and the symantics. I came back to let everyone know that I discovered the code works if I change line 31 to:
myJLabel.setText("Slider has been changed to: " + myJSlider.getValue());
Thanks, Charles
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
Thank you. I am sorry to have appeared rude about the posts you couldn't have seen yet. Pleased to see it is working. CR
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.