0

I am creating a simple JavaFX program to display an alert box/window by passing parameters from one stage to another. Below given are my code for Main class, Controller class, AlertBox class and all its FXML codes. I simply want to call the display function( which is defined in AlertBox ) in Controller class with two parameters- Title and message. I'm using Scene Builder to build the UI of the program but in the AlertBox class, I'm unable to use the label and button in the display function. I'm getting this error:
Non-static method 'mess' cannot be referenced from static context
Somebody please tell me how to solve it so I can communicate between the stages.

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

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));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

sample.fxml

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="button" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" text="Go to Scene 2" />
      <Label fx:id="label" layoutX="279.0" layoutY="236.0" style="-fx-background-color: #eee;" text="This is Scene 1!" />
      <CheckBox layoutX="156.0" layoutY="86.0" mnemonicParsing="false" text="CheckBox" />
      <Button fx:id="alert" layoutX="274.0" layoutY="295.0" mnemonicParsing="false" text="Open Alert" />
   </children>
</AnchorPane>

Controller.java

    package sample;

import sample.AlertBox;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.*;
import javafx.scene.*;

public class Controller implements Initializable{

    @FXML
    Label label;
    @FXML Button alert;

    @Override
    public void initialize(URL url, ResourceBundle rb){

        alert.setOnAction(even ->
                {
                    AlertBox.display("Title", "This is the message");
                }

        );


    }

}

AlertBox.java

    package sample;


import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class AlertBox implements Initializable{
    @FXML Button winClose;
    @FXML Label mess;
    @FXML AnchorPane lay;


    @FXML
    public static void display(String title, String message){
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        mess.setText(message);
        winClose.setOnAction(e -> window.close());

            window.setScene(new Scene(lay));
            window.showAndWait();
 }


    @Override
    public void initialize(URL location, ResourceBundle resources) {}
}

AlertBox.fxml

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane fx:id="lay" minHeight="250.0" minWidth="250.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.AlertBox">
   <children>
      <Button fx:id="winClose" layoutX="69.0" layoutY="179.0" mnemonicParsing="false" text="Close the Window" />
      <Label fx:id="mess" layoutX="109.0" layoutY="120.0" prefHeight="17.0" prefWidth="68.0" />
   </children>
</AnchorPane>
Aman Sharma
  • 35
  • 11
  • Why `implements Initializable` in the `Alert` class? – SedJ601 Feb 21 '18 at 17:39
  • This doesn't make any sense. If `AlertBox` is the controller for an FXML file, you need to load the FXML file. No matter what is static (and *nothing* here should be static), how are `mess` and `winClose` going to be initialized if you don't load the FXML? – James_D Feb 21 '18 at 17:39
  • Okay, I see why after looking at your code. You have some strange stuff going on here. You have an `FXML` for the `Alert` Class, but in the method `display()`, you seem to be creating a new `Stage`. – SedJ601 Feb 21 '18 at 17:42
  • @Sedrick Yes, What I want to do here is, On the `Controller` class I'm linking a button to the method in `Alertbox` class and when The button is clicked, It'll make a new stage with `AlertBox.fxml` loaded on it. Since, the function is parameterized, I'm passing `title` and `message` for the box. – Aman Sharma Feb 22 '18 at 03:40
  • @James_D That's why I'm using `implements Initializable` to use the `@FXML` components – Aman Sharma Feb 22 '18 at 03:42

0 Answers0