5579

How to show make a hidden form visible in javafx

Question:

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.

<a href="https://i.stack.imgur.com/IW9e5.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/IW9e5.png" data-original="https://i.stack.imgur.com/IW9e5.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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.

Answer1:

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.

<blockquote>

Main

</blockquote> 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); } } <blockquote>

Controller - MainScene

</blockquote> 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); } } } <blockquote>

FXML - Main Scene

</blockquote> <?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> <blockquote>

Controller - PopUp

</blockquote> 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(); }); } } <blockquote>

FXML - PopUp

</blockquote> <?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>

Recommend

  • JavaFx buttons click in order then play
  • How to install the specific version of postgres?
  • How to add Websphere Application Server Liberty Profile runtime dependencies to Maven POM?
  • How do you use PowerShell to extract Epub meta data (XML)?
  • Mojo::UserAgent get() with userdefined callback
  • Displaying objects using foreach loop
  • How to allow CKEditor within WebView to access the clipboard?
  • Android HTTP Authentication
  • How to integrate a custom GraphicsItem into a QML scene?
  • How do I run a function on a specific key press in JavaFX?
  • Pubnub subscribe stops receiving messages after some time
  • Download an image from url and opening it in an image control in wp7
  • Why does the address of an object change across methods?
  • SceneKit: Is it possible to cast an shadow on an Transparent Object?
  • How to reduce the time delay to reach run method of Runnable class using ExecutorService Java
  • Undefined element when tab is not active
  • IBM DevOps Pipeline: How to Access Artifacts from Previous Job?
  • Is there anything like an Objective-C block in Java?
  • is this code truly private? (python)
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • Reloading table causes flickering
  • Can you build a truly RESTful service that takes many parameters?
  • Parenthesis() and SQL Query Performance
  • Why doesn't a local variable live long enough for thread::scoped?
  • How to display a tooltip according to mouse position? - JavaFX
  • Action Pack components in Rails
  • xcode don't localize specific strings
  • Force show.bind execution
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Disable Enter in editText android
  • Highlight and Bold text in JTextPane
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • failed to connect to specific WiFi in android programmatically