473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 18 '05 #1
0 3736

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Midtgaard Andersen | last post by:
Hallo Is it possible to move a knob in jslider with data from a file and at the same time display the actual value in the knob. Or is there another way to do this e.g. with netbeans. A...
1
by: Yogi_Bear_79 | last post by:
I'm trying to set the value of a registry value that contains a rather large hex value. Can someone look at my code and show me where I am wrong? The below code appears to work, sort of. But I...
0
by: Oliver | last post by:
hi - I'm trying to set Properties dynamically (web forms). The problem is that I can't seem to set the property value based on a string - even though I know the Type of the Property. I seem...
3
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
2
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi...
5
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and...
2
by: gbayo1 | last post by:
Hi, I was just wondering would anybody be able to show us how to use a button, that when pressed, automatically scrolls a JSlider across say from 0 - 100 at a certain speed or whatever. If anyone...
9
by: FourEyes | last post by:
I've read that Reflection is slow and shouldn't be used when performance is a concern. In my case, performance is definitely a concern, so I was thinking that I could collect the PropertyInfo...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.