JSlider shud fire change event only when it comes to rest
SathishVJ
Greenhorn
Joined: Sep 07, 2000
Posts: 16
posted
0
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
posted
0
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
posted
0
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
posted
0
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>