Results 1 to 10 of 10
  1. #1
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Detect when user have changed the value of slider

    I was having problems with sliders. Now I see the (official) solution of an exercise (16.24) in "Liang, Introduction to Java Programming", which has similar problems, argh!

    This is a simple media player demo that uses the media player of javafx. A slider that controls when we are in the play is provided. It is supposed to change automatically to reflect the current state while the media is playing and the user can change the value explicitly by dragging the slider. The solution proposed bind mediaPlayer.currentTimeProperty() to the slider.valueProperty(). It adds a listener to slider.valueProperty() that detect slider.isValueChanging() and if yes call mediaPlayer.seek(...) accordingly. The test slider.isValueChanging() is here (I suppose) to distinguish the user making an explicit change and changes that are done automatically by the listener when the media plays. But this is not reliable. You can change the slider explicitly, not by dragging the slider but by clicking a new position or by typing the cursors (<- and ->) keys. In these cases slider.isValueChanging() remains false and the application does not make the change. Moreover changing the media player continuously while dragging the slider is not a good idea.

    What I would like is a way that can detect when the user has changed the slider explicitly and in that case only call the mediaPlayer.seek(...). I do not find a way to distinguish changes to the slider that are made by the user from the ones that are made by the listener while the media play. The only thing I can think of is to react explicitly to the mouse and key events from the slider but I fear in will be a not very reliable ugly hack. An idea?

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Detect when user have changed the value of slider

    I might misunderstand you intentions, but i would get rid of the .isValueChanging() condition. The Listener should take care of all changes of the slider value. Something like
    Java FX Code:
    slider.valueProperty().addListener(new ChangeListener() {
                @Override
                public void changed(ObservableValue arg0, Object arg1, Object arg2) {
                    // your action here, change, bind, whatever
                }
            });

  3. #3
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Detect when user have changed the value of slider

    @benji2505. This si a slider that should behave like the one you see in many media players (take youtube as an example). The slider is automatically updated to reflect the current positions of the video that is playing and that the user can use to explicitly change the position.

    1) If the user changes the position, the change listener will seek() the video to the corresponding time.
    2) But the media player itself change the position (with a listener on the mediaPlayer.currentTimeProperty()) to reflect the current position while it is playing. Obviously, these changes shouldn't trigger the listener that call the seek method.

    The problem is that I see no possibility to distinguish the two. I can't differentiate a change of the slider due to the user or due to the program itself. Also, it would be good to take action only when the user has finished dragging the slider, not constantly when is he dragging the slider.
    Last edited by diolu; 03-01-2017 at 04:14 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Detect when user have changed the value of slider

    As the video advances, can't you simply set the value of the slider without generating an event? Or you might use some boolean to control the source of the slider update.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Detect when user have changed the value of slider

    @jim829. The problem is that if I set the value, it automatically trigger the listener set on slider.valueProperty(). I have not seen any way to change the value without triggering it. If you have a solution... I do not see any difference between the two events that I can test.

    I see some ugly hacks, though (like to test to the amount of the difference between the old and new value) but no clean solution. But I am only a beginner, if you know something that I don't know... I suspect it is no so obvious because the author of this best-selling book proposes an official solution that has precisely these problems.
    Last edited by diolu; 03-01-2017 at 04:49 PM. Reason: typo

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Detect when user have changed the value of slider

    you can also set Listener on the node directly and not its valueProperty. This way you handle clicking, dragging, keyEvents etc. Example:
    Java FX Code:
    slider.setOnMouseReleased((MouseEvent event) -> {
        //have some fun here
    });

  7. #7
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Detect when user have changed the value of slider

    @benji2505 Yes, as a last resort I can hack that. But we can move this slider with the mouse, with the keyboard and what else? I would like to have something that makes abstraction of what the user has exactly done. But maybe there is no clean solution. Probably reacting to mouse release and key release will be enough.

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Detect when user have changed the value of slider

    You are not limited to the one event I mentioned above, I quickly counted 30 "normal" and about 10 touchscreen events that could be relevant. Includes any keyEvents, mouseDragged, etc. etc.. Just check the API and look at the methods inherited from Node and that start with setOn...
    Maybe you are looking for the setOnDragExited event? I Don't think there is this one event you can use for the slider node that covers everything, for that they implemented the changeEventListener on the valueProperty.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Detect when user have changed the value of slider

    I don't do JavaFX, but in Swing I would set a boolean field before making a programmatic change, and reset it at the end of such change. In the listener code, I would respond only if this is not a programmatic change. Something like
    Java FX Code:
    private boolean progammaticChange;
    :
    :
        progammaticChange = true;
        slider.setValue(...);
        progammaticChange = false;
    :
    :
    :
        // in the listener event handling code
        if (!programmaticChange) {
          // handle the change appropriately
        }
    I can't differentiate a change of the slider due to the user or due to the program itself.
    You see, you can!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Detect when user have changed the value of slider

    @DarrylBurke. Yes, this simple trick works. I am still a little confused about the general control flow of a JavaFX application. But there seem to be no problems here.

Similar Threads

  1. Replies: 7
    Last Post: 05-09-2014, 10:38 AM
  2. Using Java to detect user clicks on an application
    By msteele in forum New To Java
    Replies: 0
    Last Post: 10-18-2013, 02:50 AM
  3. how to focus dojo slider to a particular value and then disabling dojo slider so that
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-05-2011, 03:21 PM
  4. Replies: 1
    Last Post: 05-05-2011, 01:47 PM
  5. Detect when the user scrolls a text control
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •