This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JSlider shud fire change event only when it comes to rest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider shud fire change event only when it comes to rest" Watch "JSlider shud fire change event only when it comes to rest" New topic
Author

JSlider shud fire change event only when it comes to rest

SathishVJ
Greenhorn

Joined: Sep 07, 2000
Posts: 16
Hi,
What should I do to force the JSlider component to fire a ChangeEvent only when the component's knob has come to rest. i.e. I do not want it to fire a slew of events while the knob of the slider is being dragged.
There are functions setValueIsAdjusting and getValueIsAdjusting(boolean) that allows us to do this. However I need help on its usage or any other method.
Thanks in advance
Sathish
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
The Component is designed to throw any and all events it generates. If you only want to know when the slider has stopped, you could test for a situation when the coordinates have stopped changing. However, this may not truely represent the point at which the user has stopped sliding the component knob. What I usually do is wait for a mouseReleased event. Then you can be certain that they have 'let go' of the slider.
Sean
SathishVJ
Greenhorn

Joined: Sep 07, 2000
Posts: 16
Hi Sean,
Thanks for the attempt but I think there is a problem there. The slider can be moved without using a mouse, i.e. with the keyboard. Handling all those events is quite a convoluted method to achieve the result.
I tried out the following code - this time using a JScrollBar which supports a AdjustmentListener.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TestScroll extends JFrame {

public TestScroll(){
Container cp = getContentPane();
JScrollBar sb = new JScrollBar();
cp.add(sb, BorderLayout.EAST);
sb.addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent e){
JScrollBar jsb = (JScrollBar) e.getAdjustable();
if (!jsb.getValueIsAdjusting())
System.out.println("Value: "+ jsb.getValue());
else System.out.println("Still moving");
}
});
}

public static void main(String args[]){
TestScroll t = new TestScroll();
t.pack();
t.show();
}
}

It works just the way I want it. But it works with the wrong component. Pls see if you can get a similar functionality for me with the JSlider.
Regds
Sathish
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
You need to use stateChanged:
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
int value = slider.getValue();

if (slider.getValueIsAdjusting() == false) {
//do stuff
}
}
Hope this helps!

<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
Oh and I forgot, you need a ChangeListener too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSlider shud fire change event only when it comes to rest
 
Similar Threads
Extending JSlider and MetalSliderUI
jslider thumb movement on clicking the component
JSlider
use my own image on the knob of a jslider
Displaying Value Label in JSlider