JSlider Question

Discussion in 'Java' started by Aaron Boxer, Oct 23, 2003.

  1. Aaron Boxer

    Aaron Boxer Guest

    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
    Aaron Boxer, Oct 23, 2003
    #1
    1. Advertising

  2. Aaron Boxer wrote:

    > 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
    John C. Bollinger, Oct 23, 2003
    #2
    1. Advertising

  3. Aaron Boxer

    Phil... Guest

    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.


    "John C. Bollinger" <> wrote in message
    news:bn8p07$6sc$...
    > Aaron Boxer wrote:
    >
    > > 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
    >
    >
    Phil..., Oct 24, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Konrad Den Ende
    Replies:
    0
    Views:
    853
    Konrad Den Ende
    Dec 12, 2004
  2. Replies:
    2
    Views:
    1,106
    Michael Rauscher
    Aug 24, 2006
  3. Replies:
    2
    Views:
    1,274
  4. Simon Andrews

    Removing numbers over JSlider

    Simon Andrews, Apr 21, 2008, in forum: Java
    Replies:
    0
    Views:
    359
    Simon Andrews
    Apr 21, 2008
  5. A Watcher

    JSlider changeEvent and Matlab

    A Watcher, Jun 26, 2008, in forum: Java
    Replies:
    0
    Views:
    357
    A Watcher
    Jun 26, 2008
Loading...

Share This Page