Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Label;
- import javafx.scene.control.Slider;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- public class SliderWithLabeledThumb extends Application {
- @Override
- public void start(Stage primaryStage) {
- Slider slider = new Slider();
- StackPane root = new StackPane(slider);
- root.setPadding(new Insets(20));
- Scene scene = new Scene(root);
- slider.applyCss();
- slider.layout();
- Pane thumb = (Pane) slider.lookup(".thumb");
- Label label = new Label();
- label.textProperty().bind(slider.valueProperty().asString("%.1f"));
- thumb.getChildren().add(label);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- public class MainViewController implements Initializable {
- @FXML private VBox root;
- private Slider timeSlider;
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- timeSlider = new Slider();
- root.getChildren.add(timeSlider);
- timeSlider.setMax(360);
- timeSlider.setMin(0);
- timeSlider.setCursor(Cursor.OPEN_HAND);
- timeSlider.applyCss();
- timeSlider.layout();
- Pane thumb = (Pane) timeSlider.lookup(".thumb");
- Label label = new Label();
- label.textProperty().bind(timeSlider.valueProperty().asString("%.1f"));
- // the following line points to thumb, which is null then… (debugger proof)
- thumb.getChildren().add(label);
- }
- }
- public class Main extends Application {
- @Override
- public void start(Stage primaryStage) {
- try {
- FXMLLoader loader = new FXMLLoader();
- loader.setLocation(getClass().getResource("MainView.fxml"));
- Parent root = loader.load();
- Scene scene = new Scene(root,800,600);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement