0

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

  • 4
    whatever you do, __don't__ use static scope! Instead, use parameter passing .. for options, see also https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml. And keep in mind that controllers must be __loaded__ (vs. instantiated) to be fully functional, that is their fields are injected. – kleopatra Jul 16 '20 at 09:22
  • For why you are getting a null pointer exception, see https://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields. Controllers really shouldn’t try to communicate with each other - you should use a MVC approach instead. See, in addition to the other links, https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – James_D Jul 16 '20 at 11:38

1 Answers1

0

I found the answer at JavaFX Stage close event handler It's as simple as adding a onHidding event handler for the new window in the original calling function