Update: In accordance with @kleopatra comment.
I want to set the text of a Button
from the String
in a TextField
from another controller upon closing of the window that contains the TextField
.
public class Controller {
@FXML Button btn1;
public void initialize(){
renderBtns();
}
public void renderBtns(){
btn1.addEventHandler(MouseEvent.MOUSE_CLICKED, new CustomButtonHandler(1));
}
}
I am using a custom handler for the Button
MOUSE_CLICKED event. (Keep in mind this is a simplified version of my code, I need to use this handler in the full code)
Here is the CustomButtonHandler:
public class CustomButtonHandler implements EventHandler {
int id;
public CustomButtonHandler(int id){
this.id = id;
}
@Override
public void handle(Event event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("smetka.fxml"));
Parent root = fxmlLoader.load();
SmetkaControl controller = fxmlLoader.getController();
controller.setId(id);
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("Сметка - Маса" + " " + id);
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setOnHiding(event1 -> {
try {
controller.exitScene();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
});
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my other - SmetkaControl
:
public class SmetkaControl {
int id;
@FXML
TextField txtField;
public void setId(int id) {
this.id = id;
}
public void initialize() {
}
public void exitScene() throws IOException, ParseException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("sample.fxml"));
fxmlLoader.load();
Controller controller = fxmlLoader.getController();
txtField.setFocusTraversable(false);
controller.btn1.setText(txtField.getText());
System.out.println(controller.btn1.getText());
}
}
sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Button fx:id="btn1" mnemonicParsing="false" text="Button" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
and smetka.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="950.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SmetkaControl">
<children>
<TextField fx:id="txtField" />
</children>
</HBox>
The Main.class is default:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
When I close the window I get the correct value in the console (from btn1.getText()) but the button remains unchanged.
Why does it give me the right value in the console, but the graphic is unchanged?
How should I do this right?