Objectmix
Tags Register Mark Forums Read

JSlider to setValue to where clicked on : Java

This is a discussion on JSlider to setValue to where clicked on within the Java forums in Programming Languages category; maybe not in the spirit of Q&A, but picked up on some archived posts from a few years ago with same problem. (with no clear solutions) when clicked on a JSlider seems to go only to the next tick increment, not to where it was clicked on. this code solved the problem for me it would be better to be placed into an extended class of JSlider that could be reused, assumptions: the slider is the only JComponent in its parent and fully occupying it (otherwise watch out for the getBounds, X value (relative to parent, I think?). public void ...


Object Mix > Programming Languages > Java > JSlider to setValue to where clicked on

Reply

 

LinkBack Thread Tools
  #1  
Old 04-15-2005, 01:59 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default JSlider to setValue to where clicked on

maybe not in the spirit of Q&A, but picked up on some archived posts
from a few years ago with same problem. (with no clear solutions)

when clicked on a JSlider seems to go only to the next tick increment,
not to where it was clicked on.

this code solved the problem for me
it would be better to be placed into an extended class of JSlider that
could be reused,

assumptions: the slider is the only JComponent in its parent and fully
occupying it (otherwise watch out for the getBounds, X value (relative
to parent, I think?).

public void mousePressed(MouseEvent e)
{
JSlider slider = (JSlider) e.getSource();
System.out.println(this.getClass().getName() + " mousePressed on
slider " + slider.getName());

// JSlider is useless in that clicking on it will increment by
ticks, not goto the location clcked on
// need to get the mouseX
// then relate this to the graphic, to get a proportion
// then set the slider to this value
Rectangle rect = slider.getBounds();
double length = rect.getWidth();
int leftX = rect.getLocation().x; // seemds to be 0 (relative to
parent)
int mouseX = e.getX();
int max = slider.getMaximum();
double ratio = mouseX/length;
double value = max*ratio;
int valueI = (int)value; // the value to set the slider to

}


then use SwingUtiilities to set the Slider.
final int valueF = valueI;
try
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
sliderTrackDuration.setValue(valueF);
long time = (int) sliderTrackDuration.getValue(); // just
getting it back again
}
});
}
catch (Exception ex)
{
System.out.println("error" + ex.toString());
}

}

regards

Reply With Quote
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
PropertyInfo.SetValue() usenet CSharp 10 11-09-2007 09:01 AM
PropertyInfo.SetValue on new ObjectCollect usenet DOTNET 1 08-22-2007 02:50 PM
[WPF] data binding and SetValue() usenet DOTNET 2 06-16-2007 10:08 AM
moving on to jslider... usenet Java 1 04-29-2007 07:11 PM
JSlider Set knob Color usenet Java 3 11-28-2006 01:45 AM


All times are GMT -5. The time now is 05:11 PM.