JSlider interactions

F

FredK

Is there a way to set the block increment for a JSlider ?
By this I mean the change in value that occurs when the user clicks betweenthe current position and one of the ends (similar to the block or unit increment for a JScrollBar). I can't find anything to do this in JScrollBar orBoundedRangeModel.

Or is it a UI-specific amount (I am using Nimbus).

For an integer slider, I want to drag the knob to the approximate position,and then be able to click near it to move one integer at a time. It appears that the default amount is 10% of the range, so when the range is from 0 to 40, clicking in the "trough" area moves the value by 4.
 
J

John B. Matthews

FredK said:
Is there a way to set the block increment for a JSlider ? By this I
mean the change in value that occurs when the user clicks between the
current position and one of the ends (similar to the block or unit
increment for a JScrollBar). I can't find anything to do this in
JScrollBar or BoundedRangeModel.

Or is it a UI-specific amount (I am using Nimbus).

For an integer slider, I want to drag the knob to the approximate
position, and then be able to click near it to move one integer at a
time. It appears that the default amount is 10% of the range, so when
the range is from 0 to 40, clicking in the "trough" area moves the
value by 4.

This handled by an instance of BasicSliderUI.TrackListener, for which
you'll need a custom UI delegate, as shown here,

<http://stackoverflow.com/a/518672/230513>

and in How to Write a Custom Swing Component:

<https://today.java.net/pub/a/today/2007/02/22/how-to-write-custom-swing-component.html>

Interestingly, BasicSliderUI.ScrollListener is public, apparently to
work around an IE bug.

As an aside, the API for each says.

Instantiate it only within subclasses of .

while the source says,

Instantiate it only within subclasses of <Foo>.
 
F

FredK

This handled by an instance of BasicSliderUI.TrackListener, for which

you'll need a custom UI delegate, as shown here,



<http://stackoverflow.com/a/518672/230513>



and in How to Write a Custom Swing Component:



<https://today.java.net/pub/a/today/2007/02/22/how-to-write-custom-swing-component.html>



Interestingly, BasicSliderUI.ScrollListener is public, apparently to

work around an IE bug.



As an aside, the API for each says.



Instantiate it only within subclasses of .



while the source says,



Instantiate it only within subclasses of <Foo>.

I tried:
slider.setUI( new BasicSliderUI( slider ) {
@Override
protected void scrollDueToClickInTrack( int direction ) {
int value = slider.getValue();
value += direction;
slider.setValue( value );
}
} );

which works, but gives me a Basic LAF. The rest of my app is using Nimbus.
I haven't been able to figure out how to find or use a Nimbus SliderUI.
When I call slider.getUI(), it returns an instance of javax.swing.plaf.synth.SynthSliderUI.
If I change BasicSliderUI in the above code with SynthSliderUI, I get
an error saying that that class is not visible.
(Using Java 1.6)
 
J

John B. Matthews

FredK said:
[...]

I tried:
slider.setUI( new BasicSliderUI( slider ) {
@Override
protected void scrollDueToClickInTrack( int direction ) {
int value = slider.getValue();
value += direction;
slider.setValue( value );
}
} );

which works, but gives me a Basic LAF. The rest of my app is using
Nimbus. I haven't been able to figure out how to find or use a Nimbus
SliderUI. When I call slider.getUI(), it returns an instance of
javax.swing.plaf.synth.SynthSliderUI. If I change BasicSliderUI in
the above code with SynthSliderUI, I get an error saying that that
class is not visible. (Using Java 1.6)

Interestingly, javax.swing.plaf.synth.SynthSliderUI has package-private
access in OpenJDK 6:

<http://grepcode.com/file/repository...b14/javax/swing/plaf/synth/SynthSliderUI.java>

but public access in OpenJDK 7:

<http://download.java.net/jdk7/archive/b126/docs/api/javax/swing/plaf/synth/SynthSliderUI.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top