• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSlider shud fire change event only when it comes to rest

 
SathishVJ
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and I forgot, you need a ChangeListener too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic