JavaFX vertical slider


Question

I'm exploring JavaFX and I must say I'm little disappointed with the lack of learning material. I'm rookie to JavaFX but I have some experience with Swing. How can I place the slider vertically? And one more question,I want the Thumb to be draggable only to the Tick marks,not in the intermediate space,how can I achieve that?

slider

1
0
11/6/2017 9:57:09 AM

Accepted Answer

How can I place the slider vertically?

Use setOrientation

slider.setOrientation(Orientation.VERTICAL);

I want the Thumb to be draggable only to the Tick marks,not in the intermediate space,how can I achieve that?

Use setSnapToTicks after setting an appropriate tick unit, count and block increment.

slider.setMajorTickUnit(0.25f);
slider.setMinorTickCount(1);
slider.setBlockIncrement(0.125f);
slider.setSnapToTicks(true);

Sample app:

import static javafx.application.Application.launch;
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.Orientation;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class VerticalSliderSample extends Application {
  @Override public void start(Stage stage) {
    Slider slider = new Slider(0, 1, 0.5);
    slider.setShowTickMarks(true);
    slider.setShowTickLabels(true);
    slider.setMajorTickUnit(0.25f);
    slider.setMinorTickCount(1);
    slider.setBlockIncrement(0.125f);
    slider.setSnapToTicks(true);

    slider.setOrientation(Orientation.VERTICAL);

    VBox layout = new VBox(10);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    layout.getChildren().setAll(slider);
    stage.setScene(new Scene(layout));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}

enter image description here

I'm exploring JavaFX and I must say I'm little disappointed with the luck of learning material.

Email (constructive) feedback on Oracle developed JavaFX documentation to:

jfx-docs-feedback_ww@oracle.com

4
3/4/2013 8:59:34 PM

With Java 8 and with it JavaFX 8 and its corresponding SceneBuilder coming (at this time of post its still Java 7 GA with SceneBuilder 2.2), people should get more familiar using it. It is a powerful tool for people who don't know each special attribute from its hat off and want to practice the Inversion of Control principle the new JavaFX and its FXMLs offer us.

Here is an example out of one of my projects to show the most popular properties on each Node

enter image description here

Though I can sometimes understand you, for being frustrated especially if some new features come out, that some higher class examples what this Node can do and maybe find it easier, but as a programmer you need to learn to work with the corresponding API. It would be nice to have a part of each API documentation like in the PHP community where you can maintain it as a wiki.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon