[英]Getters for FXML controllers in the controller class instead of lookup?
[英]How to lookup the thumb of a slider in a controller class for fxml?
我想自定義一個Slider
並在 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 視圖中的滑塊的拇指,在嘗試將Label
添加到thumb
的孩子時,我總是會得到一個NullPointerException
......顯然無法查找拇指。 然后我在這里找到了另一個答案,說lookup()
必須在stage.show()
之后stage.show()
,所以這里又是我的問題:
如何在管理 fxml 組件(沒有stage.show()
)的控制器類中查找滑塊拇指?
我嘗試僅在代碼中創建一個Slider
,也使用了 fxml 中的一個,在控制器的initialize(…)
方法中調用時,兩個查找都返回null
。
是否可以在控制器類中自定義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>
將查找代碼放在滑塊偵聽器中,如下所示:
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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.