Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX标题窗格查找(.title)返回null_Java_Javafx - Fatal编程技术网

JavaFX标题窗格查找(.title)返回null

JavaFX标题窗格查找(.title)返回null,java,javafx,Java,Javafx,我是JavaFX新手,正在创建一个有趣的应用程序。我试图动态添加一个标题窗格,在大约70%的时间里,当我试图查找标题窗格的标题时,会出现空指针异常。我试图为我的问题创建一个简单的演示,但无法在我的应用程序之外重现该问题,但我可以解决我的问题。我希望有人能帮助我理解为什么我的解决方案有效,也许能为我指明更好的解决方案的方向。我正在使用带有控制器的FXML文件。我试图在Platform.runLater()中查找标题,因为我正在手动编辑标题的布局和元素。在控制器的初始化函数中,我执行以下操作以获取空

我是JavaFX新手,正在创建一个有趣的应用程序。我试图动态添加一个
标题窗格
,在大约70%的时间里,当我试图查找
标题窗格
的标题时,会出现空指针异常。我试图为我的问题创建一个简单的演示,但无法在我的应用程序之外重现该问题,但我可以解决我的问题。我希望有人能帮助我理解为什么我的解决方案有效,也许能为我指明更好的解决方案的方向。我正在使用带有控制器的FXML文件。我试图在Platform.runLater()中查找标题,因为我正在手动编辑标题的布局和元素。在控制器的初始化函数中,我执行以下操作以获取空指针异常:

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        titledpane.lookup(".title"); // will return null about 70% of the time
    }
});
// Do more stuff
然而,如果我将该调用封装在一个计时器中,并在500毫秒内执行它,它似乎永远不会返回Null

new java.util.Timer().schedule(new java.util.TimerTask() {
    @Override
    public void run() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                titledpane.lookup(".title"); // doesn't seem to return null
            }
        });
    }
}, 500);

一个论坛提到,在调用标题查找之前,CSS必须应用于元素,因此我尝试手动将CSS应用于标题,但
titledpane.lookup(.title”)
仍然返回空值。有人能帮我理解这里发生了什么吗?提前谢谢

你应该读这篇文章

下面是一个关于如何动态加载标题窗格的示例。每次单击“添加任务”按钮时,都会添加一个标题窗格

Task.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<fx:root collapsible="false" prefHeight="72.0" prefWidth="202.0" text="Task" type="TitledPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <content>
      <Pane prefHeight="43.0" prefWidth="200.0">
         <children>
         </children>
      </Pane>
   </content>
</fx:root>
Demo.java

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Demo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        Button addTaskButton = new Button( "Add Task");

        addTaskButton.setOnAction(new EventHandler<ActionEvent>() {

            double x=0;
            double y=0;
            int count=0;

            @Override
            public void handle(ActionEvent event) {

                // calculate new position
                x+=50;
                y+=50;

                // task counter
                count++;

                Task task = new Task( "Task " + count);
                task.relocate(x, y);
                root.getChildren().add( task);

            }
        });
        root.getChildren().add( addTaskButton);


        Scene scene = new Scene( root, 1024, 768);

        primaryStage.setScene( scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

我也有同样的问题。我通过在包含
标题窗格的窗格上调用
applyCss()
layout()
解决了这个问题:

Node loadedPane = paneLoader.load();
bodyPane.setCenter(loadedPane);
bodyPane.applyCss();
bodyPane.layout();

你到底想达到什么目的?我认为查找不适合您。@Lorand在标题窗格的标题中,我试图右对齐图像,同时保持文本在左侧。我可以通过查找title元素并进行一些计算来实现这一点,但是titledpane.lookup(“.title”)有时会返回null,如问题中所述。我更新了我的示例,请参见下面的代码。applyCss()和layout()为我完成了这项工作。谢谢
public class Task extends Region {

    TitledPane titledPane;

    public Task( String title) {

        final FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Task.fxml"));

        titledPane = new TitledPane();

        fxmlLoader.setRoot( titledPane);
        fxmlLoader.setController( this);

        try {
          fxmlLoader.load();
        } catch( IOException exception) {
          throw new RuntimeException( exception);
        }


        // create custom title with text left and icon right
        AnchorPane anchorpane = new AnchorPane();
        double offsetRight = 20; // just an arbitrary value. usually the offset from the left of the title
        anchorpane.prefWidthProperty().bind(titledPane.widthProperty().subtract( offsetRight));

        // create text for title
        Label label = new Label( title);

        // create icon for title
        Image image = new Image( getClass().getResource( "title.png").toExternalForm());
        ImageView icon = new ImageView();
        icon.setImage(image);

//      Rectangle icon = new Rectangle(16, 16);

        // set text and icon positions
        AnchorPane.setLeftAnchor(label, 0.0);
        AnchorPane.setRightAnchor(icon, 0.0);

        // add text and icon to custom title
        anchorpane.getChildren().addAll( label, icon);

        // set custom title
        titledPane.setGraphic( anchorpane);

        // show only our custom title, don't show the text of titlePane
        titledPane.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

        getChildren().add( titledPane);

    }

}
Node loadedPane = paneLoader.load();
bodyPane.setCenter(loadedPane);
bodyPane.applyCss();
bodyPane.layout();