• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JSlider shud fire change event only when it comes to rest

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh and I forgot, you need a ChangeListener too.
 
Well THAT's new! Comfort me, reliable tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic