I have a certain problem with recreating a WinAPI-like class (MessageBox).
This java class provided below is supposed to deliver short messages to the user.
FXML file was generated in SceneBuilder.
Frankly, I have no clue why updating textArea field doesn't update the window with provided message ('String').
Java (not working):
import Util.WindowURLProvider; // this is a custom class which provides URL links to FXML pages
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.io.IOException;
public class StatusScreen {
private static Stage statusStage = new Stage();
private static WindowURLProvider windowURLProvider = new WindowURLProvider();
public TextArea textArea;
public Button okButton;
public StatusScreen() {
}
public void setScreen(String title, String textToShow) throws IOException {
textArea = new TextArea();
textArea.appendText(textToShow); // the problem area
statusStage.setTitle(title);
statusStage.setScene(
new Scene(
FXMLLoader.load(windowURLProvider.getStatusWindowURL())
)
);
statusStage.show();
}
public void onOkButtonPressed(ActionEvent actionEvent) {
statusStage.close();
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane prefHeight="150." prefWidth="200.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.Screens.StatusScreen">
<children>
<GridPane prefHeight="150.0" prefWidth="200.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="112.0" minHeight="10.0" prefHeight="112.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="70.0" minHeight="10.0" prefHeight="38.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="okButton" mnemonicParsing="false" onAction="#onOkButtonPressed" prefHeight="25.0" prefWidth="213.0" text="OK" GridPane.rowIndex="1" />
<TextArea fx:id="textArea" focusTraversable="false" prefHeight="200.0" prefWidth="200.0" promptText="SAMPLE_TEXT" text="" wrapText="true" />
</children>
</GridPane>
</children>
</AnchorPane>
EDIT:
Java (working):
Better solutions are down in the comment section :)
This example uses scene's lookup method to get textArea object reference
import Util.WindowURLProvider;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.io.IOException;
public class StatusScreen {
private static Stage statusStage = new Stage();
private static WindowURLProvider windowURLProvider = new WindowURLProvider();
public TextArea textArea;
public Button okButton;
public StatusScreen() {
}
public void setScreen(String title, String textToShow) throws IOException {
Scene scene = new Scene(FXMLLoader.load(windowURLProvider.getStatusWindowURL()));
textArea = (TextArea) scene.lookup("#textArea");
textArea.appendText(textToShow);
statusStage.setTitle(title);
statusStage.setScene(scene);
statusStage.show();
}
public void onOkButtonPressed(ActionEvent actionEvent) {
statusStage.close();
}
}