javafx 8,未使用fxml,scene.lookup未找到节点
问题描述:
在以下代码中,primaryScene.lookup(第30行)返回null。但在调试模式(IntelliJ)中,我可以钻入并查看场景图中的节点。javafx 8,未使用fxml,scene.lookup未找到节点
这是一个独立的例子。没有FXML。
该应用只是绘制舞台和场景,然后尝试使用Scene.lookup选择标记为id =“drawPane”的窗格。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import static javafx.application.Application.launch;
public class Main extends Application {
Scene primaryScene;
Pane drawPane;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryScene = buildUI(primaryStage);
if (primaryScene == null) throw new NullPointerException();
drawPane = (Pane)primaryScene.lookup("#drawPane");
if (drawPane == null) {
throw new NullPointerException();
}
primaryStage.show();
}
private Scene buildUI(Stage primaryStage) {
ScrollPane scrollPane;
ImageView iv;
BorderPane root = new BorderPane();
Scene primaryScene = new Scene(root, 900, 800);
initializePrimaryStage(primaryStage, primaryScene);
initializeFrameContent(root);
initializeContent(root);
return primaryScene;
}
private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
primaryStage.setTitle("Image Clip Test");
primaryStage.setScene(primaryScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.minWidthProperty().setValue(400);
primaryStage.minHeightProperty().setValue(300);
}
private void initializeFrameContent(BorderPane root) {
Button leftButton = new Button("LEFT");
leftButton.setId("leftButton");
Button rightButton = new Button("RIGHT");
rightButton.setId("rightButton");
Button topButton = new Button("TOP");
rightButton.setId("topButton");
StackPane leftPane = new StackPane(leftButton);
leftPane.setAlignment(Pos.TOP_LEFT);
StackPane rightPane = new StackPane(rightButton);
rightPane.setAlignment(Pos.TOP_RIGHT);
root.setLeft(leftPane);
root.setTop(topButton);
root.setRight(rightButton);
}
private void initializeContent(BorderPane root) {
Pane drawPane = new Pane();
drawPane.setId("drawPane");
drawPane.setPrefSize(1000, 800);
drawPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
ScrollPane scrollPane = new ScrollPane(drawPane);
scrollPane.setPrefSize(300, 300);
scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
scrollPane.setStyle("-fx-focus-color: transparent");
root.setCenter(scrollPane);
}
}
答
在应用CSS之前查找不起作用,这通常不会发生,直到场景布局并呈现在屏幕上。
下面将迫使这更早发生,这样你就可以访问查找:
public void start(Stage primaryStage) {
primaryScene = buildUI(primaryStage);
if (primaryScene == null) throw new NullPointerException();
primaryScene.getRoot().applyCss();
drawPane = (Pane)primaryScene.lookup("#drawPane");
if (drawPane == null) {
throw new NullPointerException();
}
答
奇怪,但调用new ScrollPane(drawPane)
的drawPane不添加到滚动列表儿童。我尝试了scrollPane.setContent(drawPane)
,但它也不起作用。这就是为什么你不能查看它的ID。但是,在使用以下代码显示窗口后,您可以通过它的ID来查找drawPane
。
Platform.runLater(() -> {
drawPane = (Pane)primaryScene.lookup("#drawPane");
if (drawPane == null) {
throw new NullPointerException(); // this will not throw
}
});
哇。我一直在Google上搜索,没有提到这个小块。谢谢! – vocalionecho
不知道这个,非常感谢 – stjepano