aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ChangeListeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ChangeListeners" Watch "ChangeListeners" New topic
Author

ChangeListeners

Charles Souders
Greenhorn

Joined: Apr 25, 2006
Posts: 5
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:
________________________________________________

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;


public class SliderTest extends JFrame implements ChangeListener
{
JSlider myJSlider = new JSlider(JSlider.HORIZONTAL, 0, 50, 20);
JLabel myJLabel = new JLabel("Default Text");

SliderTest()
{
super("SliderTest");
setSize(600, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);

add (myJSlider);
add (myJLabel);
myJSlider.addChangeListener(this);

setVisible(true);
}

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: 39396
    
  28
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
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: 39396
    
  28
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://aspose.com/file-tools
 
subject: ChangeListeners