How to switch stages in JavaFX -




i have login stage (300 x 250), want open main stage (fullscreen) if credentials correct.

i have figured out how check login credentials, how can close login stage , open stage?

if application supposed work in 1 window prefer using gui manager singleton class, manages changing windows. below provided complete code of simple application uses mechanism. let's assume files in 1 package, called sample.

main.java - initialize javafx components here:

package sample;  import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.layout.stackpane; import javafx.stage.stage; import java.io.ioexception;  public class main extends application {      @override     public void start(stage primarystage) throws exception {       fxmlloader loader = new fxmlloader();       loader.setlocation(main.class.getresource("/sample/root.fxml"));       try{         stackpane rootpane;         rootpane = loader.load();         guimanager guimodel = guimanager.getinstance();         guimodel.setrootpane(rootpane);         scene scene = new scene(rootpane);         primarystage.setscene(scene);         primarystage.show();         guimodel.changewindow("/sample/firstwindow.fxml");       } catch (ioexception exception) {         exception.printstacktrace();       }       }      public static void main(string[] args) {         launch(args);     }  } 

root.fxml - windows supposed based on it:

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.layout.stackpane?>  <stackpane fx:id="rootpane"            xmlns="http://javafx.com/javafx/8.0.60"            xmlns:fx="http://javafx.com/fxml/1"            prefwidth="1" prefheight="1"/> 

firstwindow.fxml - first actual window displayed:

<?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.layout.vbox?>  <vbox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.firstwindowcontroller">     <label text="first window"/>     <button text="change window" onaction="#changewindow"/> </vbox> 

firstwindowcontroller.java - controller class of first window:

package sample;  import javafx.fxml.fxml;  public class firstwindowcontroller {    @fxml   private void changewindow() {     guimanager.getinstance().changewindow("/sample/secondwindow.fxml");   } } 

secondwindow.fxml - displayed after clicking button of first window:

<?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.layout.vbox?>  <vbox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.secondwindowcontroller" >     <label text="second window"/>     <button text="change window" onaction="#changewindow"/> </vbox> 

secondwindowcontroller.java - controller class of second window:

package sample;  import javafx.fxml.fxml;  public class secondwindowcontroller {    @fxml   private void changewindow() {     guimanager.getinstance().changewindow("/sample/firstwindow.fxml");   } } 

guimanager.java - class manages changing windows based on root:

package sample;  import javafx.collections.observablelist; import javafx.fxml.fxmlloader; import javafx.scene.node; import javafx.scene.layout.pane; import javafx.scene.layout.stackpane; import javafx.stage.stage; import javafx.stage.window; import java.io.ioexception;  public class guimanager {    private stackpane rootpane;   private static guimanager instance;    public static guimanager getinstance() {     if (instance == null) {       instance = new guimanager();     }     return instance;   }    private guimanager() {}    public void changewindow(string path) {     changewindow(rootpane, path, this);     rootpane.setprefwidth(-1);     rootpane.setprefheight(-1);   }    public static void changewindow(pane pane, string newwindowpath, object callingcontroller) {     window window = pane.getscene().getwindow();     double x = window.getx() + gethorizontalmidpoint(window);     double y = window.gety() + getverticalmidpoint(window);      observablelist<node> childrenlist = pane.getchildren();     removeallincludedchildren(childrenlist);      fxmlloader loader = new fxmlloader(callingcontroller.getclass().getresource(newwindowpath));      try {       pane.getchildren().add(loader.load());       stage primarystage = (stage) window;       primarystage.setminheight(0);       primarystage.setminwidth(0);       window.sizetoscene();       window.setx(x - gethorizontalmidpoint(window));       window.sety(y - getverticalmidpoint(window));       primarystage.setminheight(window.getheight());       primarystage.setminwidth(window.getwidth());     } catch (ioexception exception) {       exception.printstacktrace();     }   }    private static double gethorizontalmidpoint(window window) {     int horizontalbisectioncoefficient = 2;     return window.getwidth() / horizontalbisectioncoefficient;   }    private static double getverticalmidpoint(window window) {     int verticalbisectioncoefficient = 2;     return window.getheight() / verticalbisectioncoefficient;   }    private static void removeallincludedchildren(observablelist<node> childrenlist) {     (int childindex = 0; childindex < childrenlist.size(); childindex++) {       childrenlist.remove(childindex);     }   }    public void setrootpane(stackpane rootpane) {     this.rootpane = rootpane;   } } 




wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

Jenkins: find build number for git commit -

firebase - How to wait value in Ionic 2 -