javafx 8,不使用fxml,scene.lookup找不到节点

时间:2016-02-19 17:39:42

标签: java javafx

在以下代码中,primaryScene.lookup(第30行)返回null。但是在调试模式(IntelliJ)中,我可以钻取并查看场景图中的节点。

这是一个独立的例子。没有FXML。

应用程序只绘制舞台和场景,然后尝试选择标记为id =" drawPane"的窗格。使用Scene.lookup。

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

2 个答案:

答案 0 :(得分:6)

在应用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();
}

答案 1 :(得分:-1)

奇怪但是对new ScrollPane(drawPane)的调用不会将drawPane添加到ScrollPane子列表中。我尝试使用scrollPane.setContent(drawPane),但它也不起作用。这就是为什么你不能通过id查找它。但是,在使用以下代码显示窗口后,您可以通过它的id查找drawPane

Platform.runLater(() -> {
  drawPane = (Pane)primaryScene.lookup("#drawPane");
  if (drawPane == null) {
    throw new NullPointerException(); // this will not throw
  }
});