1

I would like to ask you, if I can obtain a TextArea defined in my sample.fxml file from start() method, where I have defined FXMLoader and primary stage. I need to obtain the TextArea object there and set on it some String which I have saved into file before.

Thank you guys!

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

And here is my .fxml file

<?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>
vviston
  • 183
  • 1
  • 12

1 Answers1

0

get the node by id with forEach loop then cast the node into TextArea

 root.getChildrenUnmodifiable().forEach((node) -> {
        if(node.getId()=="timeInputId"){
        TextArea textArea = (TextArea)node;
        textArea.setText(" your string here ");
        }
    });
Giovanni Contreras
  • 2,345
  • 1
  • 13
  • 22
  • 1
    It’s probably better (more efficient and more robust) to use the FXMLLoader’s namespace to get the text area. But this simply isn’t the right place to do this anyway. – James_D May 16 '20 at 19:37
  • i agree but i don't know why he needs to get the node in his Main class . – Giovanni Contreras May 16 '20 at 21:07