So I have a program that starts with a home screen that has button and a label. I want to make it so that when someone click the button, a new window pops out. And by entering some text in the text field in the new window and hit submit button, the new window closes and the text of label in the home screen is set to the user input.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="HomeController" prefHeight="400.0" prefWidth="600.0">
<VBox prefHeight="400.0" prefWidth="600.0">
<children>
<Label fx:id="label" prefWidth="100" prefHeight="50">
<VBox.margin>
<Insets top="75" left="250"/>
</VBox.margin>
</Label>
<Button fx:id="button" prefWidth="100" prefHeight="50" text="Button" onAction="#buttonClick">
<VBox.margin>
<Insets top="150" left="250"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class HomeController {
@FXML
private Button button;
@FXML
private static Label label;
public void buttonClick(ActionEvent e) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("alert.fxml"));
Stage window = new Stage();
window.initStyle(StageStyle.UNDECORATED);
window.initModality(Modality.APPLICATION_MODAL);
window.setScene(new Scene(root));
window.show();
}
public static Label getLabel() {
return label;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="AlertController" prefHeight="200.0" prefWidth="300.0">
<VBox prefHeight="200.0" prefWidth="300.0">
<children>
<TextField fx:id="text" prefWidth="200" prefHeight="25">
<VBox.margin>
<Insets top="37.5" left="50" right="50"/>
</VBox.margin>
</TextField>
<Button fx:id="submit" prefWidth="100" prefHeight="25" text="Submit" onAction="#submit">
<VBox.margin>
<Insets top="75" left="100"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class AlertController {
@FXML
private TextField text;
public void submit(ActionEvent actionEvent) {
String str = text.getText();
Stage window = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
HomeController.getLabel().setText(str);
window.close();
}
}
How do different fxml controllers communicate to each other? Since creating an instance of controller class doesn't seem to work, I try to make the label in home controller static, but when I try to update its text in another controller, a NullPointerException is thrown. Is there any other way