如何在fxml的控制器类中查找滑块的拇指?

时间:2017-08-23 09:40:18

标签: java javafx controller slider fxml

我想自定义Slider并找到一个有效的示例here on stackoverflow

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);
    }
}

不幸的是,如果我在我的控制器类中查找滑块的拇指以获取fxml视图,那么在尝试将NullPointerException添加到Label时,我将始终获得thumb的孩子......拇指显然不能被抬起头来。然后我在此处找到另一个answer,其中lookup()必须在stage.show()之后调用,所以这是我的问题:
如何在管理fxml组件的控制器类中查找滑块(没有stage.show())?
我尝试仅在代码中创建Slider并且还使用fxml中的一个,当在控制器的null方法中调用时,两个查找都返回initialize(…)。是否可以以某种方式自定义控制器类中的Slider拇指?

编辑:

产生异常的代码在控制器类中,由于相应xml文件中的条目而自动实例化。无论我是否使用通过注释从fxml加载的Slider,只需要一个成员并初始化它或创建一个新的Slider对象。只要我在控制器类中执行此操作,我将继续从以下代码中注释的行中获取NullPointerExceptions

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);
    }
}

Main.java我只是加载fxml文件:

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);
    }
}

最后,fxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="root" maxHeight="600" maxWidth="800" prefHeight="600"
    prefWidth="800" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="application.MainViewController">
</VBox>

1 个答案:

答案 0 :(得分:3)

将查找代码放在滑块侦听器中,如下所示:

volumeBar.valueProperty().addListener((observable, oldValue, newValue) -> {
        Pane thumb = (Pane) volumeBar.lookup(".thumb");
        if(thumb.getChildren().size() == 0){
            Label label = new Label();
            label.textProperty().bind(volumeBar.valueProperty().asString("%.0f"));
            thumb.getChildren().add(label);
        }
    });