conor O leary
conor O leary

Reputation: 3

How to show make a hidden form visible in javafx

I have an application that is designed to be a sandwich shop system and I have a main menu form that is my main class i.e. this class holds my main class and is the first form to open when the application is executed.

enter image description here

When I click on the create new standard order button I have it so it will display a menu of items to chose from. I have this code behind the create new standard order button so it will hide the first window and display the next window.

btnStdOrder.setOnAction(e -> { 
    ((Node)e.getSource()).getScene().getWindow().hide();
    NewOrderPopUp.Display();  
});

However, how do I go back to this first window? I have tried using the same code as above but because the first form holds my main and start methods I cant recall them again using the above method (or maybe I can I just don't know how to do it). Any help would be greatly appreciated.

Upvotes: 0

Views: 3156

Answers (1)

SedJ601
SedJ601

Reputation: 13859

If your popUpDisplay method ends with showAndWait(), you can try this:

((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
NewOrderPopUp.Display();  
((Stage)((Node)e.getSource()).getScene().getWindow()).show();

I created a sample app that shows this behavior in action.

Main

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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication53 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Controller - MainScene

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{    
    @FXML private Button btnMain;  

    Stage window;

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

        // TODO
        btnMain.setOnAction(e -> {
            ((Stage)((Node)e.getSource()).getScene().getWindow()).hide();           
            popUpDisplay();
            ((Stage)((Node)e.getSource()).getScene().getWindow()).show();

        });
    }    

    public void popUpDisplay()
    {
        try
        {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SceneTwo.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);
            //stage.initStyle(StageStyle.UNDECORATED);
            stage.setTitle("PopUp");
            stage.setScene(new Scene(root1));
            stage.showAndWait();
        }
        catch (IOException ex)
        {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

FXML - Main Scene

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

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

<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="300.0" minHeight="300.0" minWidth="300.0" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication53.FXMLDocumentController">
   <children>
      <Button fx:id="btnMain" layoutX="124.0" layoutY="248.0" mnemonicParsing="false" text="Button" />
      <Label layoutX="99.0" layoutY="135.0" text="Main Scene">
         <font>
            <Font size="20.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

Controller - PopUp

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;

/**
 * FXML Controller class
 *
 * @author blj0011
 */
public class SceneTwoController implements Initializable
{
    @FXML Button btnClosePopup;    

    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
        btnClosePopup.setOnAction(e -> {
            ((Stage)(((Button)e.getSource()).getScene().getWindow())).close();           
        });
    }    

}

FXML - PopUp

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

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


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication53.SceneTwoController">
   <children>
      <Label layoutX="257.0" layoutY="155.0" text="Popup">
         <font>
            <Font size="30.0" />
         </font>
      </Label>
      <Button fx:id="btnClosePopup" layoutX="259.0" layoutY="353.0" mnemonicParsing="false" text="Close Popup" />
   </children>
</AnchorPane>

Upvotes: 1

Related Questions