This discussion is archived
2 Replies Latest reply: May 8, 2012 11:06 AM by jsmith RSS

Bind two Sliders

931470 Newbie
Currently Being Moderated
Hi,

Allow for two sliders to move independently of each other. When a button is pressed bind them together, hence when one moves up the other moves up.. Note: the sliders need to increment the same and not both just have the same value.. Please see my source below:

Thanks for any help

public class SliderBinding extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Bind Slider Example");

HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);

final Slider slider = new Slider();
slider.setOrientation(Orientation.VERTICAL);

final Slider slider2 = new Slider();
slider2.setOrientation(Orientation.VERTICAL);

final Button bind = new Button("Bind");
bind.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event)
{
// The below is the issue as this just sets one value to another, is there anyway of moving up or down the amount that a slider has moved??
slider.valueProperty().bindBidirectional(slider2.valueProperty());
}
});


hbox.getChildren().addAll(bind,slider,slider2);
Scene scene = new Scene(hbox, 763,563);
scene.setFill(Color.BISQUE);


primaryStage.setScene(scene);
primaryStage.show();
}
}
  • 1. Re: Bind two Sliders
    shakir.gusaroff Expert
    Currently Being Moderated
    How about this:

    bind.setOnAction(new EventHandler<ActionEvent>() {
    
                @Override
                public void handle(ActionEvent event) {
    
    
    
                    slider.valueProperty().addListener(new ChangeListener<Number>() {
    
                        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                            slider2.valueProperty().set(slider2.valueProperty().get() + newValue.doubleValue() - oldValue.doubleValue() );
                                                    
                        }
                    });
    
    
                }
            });
  • 2. Re: Bind two Sliders
    jsmith Guru
    Currently Being Moderated
    The following binds a slider to a text field, but some of the concepts in it are equally applicable to binding two sliders together.
    https://gist.github.com/1962045 "     JavaFX example of binding a slider value to a Label or editable TextField. "

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points