java - 在JavaFX应用程序的Main方法中获取.fxml文件中定义的TextArea

标签 java user-interface javafx fxml

我想问你,我是否可以从start()方法获取我的sample.fxml文件中定义的TextArea,我在其中定义了FXMLLoader和primary stage。我需要在那里获取 TextArea 对象并在其上设置一些我之前保存到文件中的字符串。

谢谢大家!

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
        primaryStage.setTitle("Appilcation");
        primaryStage.setScene(new Scene(root, 420, 330));
        primaryStage.setResizable(true);
        root.getChildrenUnmodifiable().
        // get the TextArea and set text on it which I will get from file
    }

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

这是我的 .fxml 文件

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="sample.PDPController" xmlns:fx="http://javafx.com/fxml" hgap="2" vgap="3" styleClass="root">
    <padding><Insets top="5" right="15" bottom="5" left="15"/></padding>

    <Text id="welcome-text" text="Welcome user!"
          GridPane.columnIndex="0"
          GridPane.rowIndex="0"
          GridPane.columnSpan="1"
          GridPane.halignment="CENTER"
    />

    <Text id="welcome-text-secondary" text="Count data..."
          GridPane.columnIndex="0"
          GridPane.rowIndex="1"
          GridPane.columnSpan="1"
          GridPane.halignment="CENTER"
    />

    <TextArea fx:id="timeInputId" minHeight="200" minWidth="200" wrapText="true"
              promptText="Insert data in specified format." focusTraversable="false"
              GridPane.columnIndex="0"
              GridPane.rowIndex="2"
              GridPane.columnSpan="1"
              GridPane.halignment="CENTER"
    />


    <HBox spacing="10" alignment="bottom_left"
          GridPane.columnIndex="0"
          GridPane.rowIndex="5"
          GridPane.columnSpan="1">
        <Button text="Počítej" onAction="#handleSubmitButtonAction"/>
        <Button text="Ulož" onAction="#handleSaveButtonAction"/>
    </HBox>

    <Text fx:id="actionTarget"
          GridPane.columnIndex="0"
          GridPane.rowIndex="6"
          GridPane.columnSpan="1"
          GridPane.halignment="CENTER"
    />
    <stylesheets>
        <URL value="@../styles.css" />
    </stylesheets>

</GridPane>

最佳答案

使用 forEach 循环通过 id 获取节点,然后将节点转换为 TextArea

 root.getChildrenUnmodifiable().forEach((node) -> {
        if(node.getId()=="timeInputId"){
        TextArea textArea = (TextArea)node;
        textArea.setText(" your string here ");
        }
    });

关于java - 在JavaFX应用程序的Main方法中获取.fxml文件中定义的TextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61842197/

相关文章:

css - 水平 ul li 导航 Bootstrap

java - *基本* 单字符串列的 JavaFX TableView

java - FXML 文件无法打开

JavaFX + GraalVM + Gluon

java.mail 将邮件标记为未读

java - GSON 预期为 BEGIN_ARRAY,但实际为 STRING

iphone - 当另一个栏内发生某些事情时,更新 UITabBarViewController 中一个栏内的 TableView

cocoa - 有没有办法让 NSView 重绘更频繁?

java - 如何运行与 Android Studio 在提交时执行的代码分析相同的代码分析?

java - 获取没有 @SuppressWarnings ("unchecked"的通用容器)