JSlider Question

A

Aaron Boxer

Hello,

I have a JSlider that I am using to set a stored Preference called DELAY. I
have added a change listener, which calls getValue() and changes the DELAY
setting when the slider is moved.

When I first run my program, I would like to set the value of the slider to
the stored DELAY value.
Hovever, when I call setValue(), my change listener gets
called, which then does a getValue(), returning 0. So, my slider is always
set to zero at startup.

How can I programmatically set the value of my slider?

Thanks!

Aaron Boxer
 
J

John C. Bollinger

Aaron said:
Hello,

I have a JSlider that I am using to set a stored Preference called DELAY. I
have added a change listener, which calls getValue() and changes the DELAY
setting when the slider is moved.

When I first run my program, I would like to set the value of the slider to
the stored DELAY value.
Hovever, when I call setValue(), my change listener gets
called, which then does a getValue(), returning 0. So, my slider is always
set to zero at startup.

How can I programmatically set the value of my slider?

One solution would be to set the slider's initial value before you
install the ChangeListener.

The root problem, though, is that you have a data duplication. It would
be better if the slider's model and the stored preference did not
maintain seperate copies of the value. You could achieve this by giving
your slider a custom model that delegated to the Preference for its
current value. The model would register itself as a
PreferenceChangeListener on the relevant Preferences object so as to
catch changes to the value that occur via means other than the slider.
There are various efficiencies and optimizations that could be applied
in code, depending on the details of what you want to do.


John Bollinger
(e-mail address removed)
 
P

Phil...

can you use this constructor

JSlider(int orientation, int min, int max, int value)
Creates a slider with the specified orientation and the specified
minimum, maximum, and initial values.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top