"javafx new custom pop out window" Code Answer

3

i think i understand what you want, here is an (workaround) example:

  • i've created two fxml files, one for the main window(mainwindow.fxml) and one for popup window (popup.fxml)
  • created two controller classes for each fxml file, these controllers extend an abstractcontoller class, all the interesting thing goes into those two controllers.
  • the abstract controller class has just one method that allows concrete controllers to have access to the main application
  • nothing fancy in the mainapp class, just loading the controller of the mainwindow and setting mainwindow as the root of the primary stage scene.

source code on github

screenshot

mainwindow.fxml

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.anchorpane?>


<vbox alignment="center" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <label text="i'm the main here">
         <font>
            <font size="24.0" />
         </font>
      </label>
      <label text="lets pop this out">
         <font>
            <font size="18.0" />
         </font>
      </label>
      <button fx:id="popitbtn" mnemonicparsing="false" text="now">
         <font>
            <font size="14.0" />
         </font>
      </button>
      <label fx:id="resultlbl" text="i've got this (username: /password: )">
         <vbox.margin>
            <insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </vbox.margin>
      </label>
   </children>
   <padding>
      <insets bottom="40.0" left="40.0" right="40.0" top="40.0" />
   </padding>
</vbox>

popup.fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<vbox alignment="center" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <hbox alignment="center" style="-fx-background-color: #e1c1c1;">
         <children>
            <label text="popup window example" textfill="#752b2b">
               <font>
                  <font size="14.0" />
               </font>
            </label>
         </children>
      </hbox>
      <hbox alignment="center">
         <children>
            <label prefwidth="70.0" text="username" />
            <textfield fx:id="usernametf" prompttext="john doe" />
         </children>
      </hbox>
      <hbox alignment="center">
         <children>
            <label prefwidth="70.0" text="password" />
            <passwordfield fx:id="passwordpf" prompttext="*********" />
         </children>
      </hbox>
      <hbox alignment="center">
         <children>
            <button fx:id="connectbtn" mnemonicparsing="false" text="connect" />
         </children>
      </hbox>
   </children>
   <padding>
      <insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
</vbox>

abstractcontroller.java

public abstract class abstractcontroller {

    protected mainapp main;

    public void setmainapp(mainapp main) {
        this.main = main;
    }
}

mainapp.java

import javafx.application.application;
import javafx.fxml.fxmlloader;
import javafx.scene.parent;
import javafx.scene.scene;
import javafx.stage.stage;

public class mainapp extends application {
        private stage primarystage;

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

        @override
        public void start(stage primarystage) throws exception {
            this.primarystage = primarystage;

            fxmlloader loader = new fxmlloader();
            loader.setlocation(getclass().getresource("mainwindow.fxml"));
            mainwindowcontroller mainwindowcontroller = new mainwindowcontroller();
            mainwindowcontroller.setmainapp(this);
            loader.setcontroller(mainwindowcontroller);
            parent layout = loader.load();

            scene scene = new scene(layout);
            primarystage.setscene(scene);
            primarystage.show();
        }

        public stage getprimarystage() {
            return primarystage;
        }
}

mainwindowcontroller.java

    import java.io.ioexception;
    import java.net.url;
    import java.util.hashmap;
    import java.util.resourcebundle;

    import javafx.fxml.fxml;
    import javafx.fxml.fxmlloader;
    import javafx.fxml.initializable;
    import javafx.scene.parent;
    import javafx.scene.scene;
    import javafx.scene.control.alert;
    import javafx.scene.control.alert.alerttype;
    import javafx.scene.control.button;
    import javafx.scene.control.label;
    import javafx.stage.modality;
    import javafx.stage.stage;

    public class mainwindowcontroller extends abstractcontroller implements initializable {

        @fxml private button popitbtn;
        @fxml private label resultlbl;

        @override
        public void initialize(url url, resourcebundle rb) {
            resultlbl.settext("lets get something in here");
            popitbtn.setonaction((event)->{
                hashmap<string, object> resultmap = showpopupwindow();
                resultlbl.settext("i've got this (username: "+resultmap.get("username")
                        +" /password: "+resultmap.get("password")+")");
            });

        }


        private hashmap<string, object> showpopupwindow() {
            hashmap<string, object> resultmap = new hashmap<string, object>();

            fxmlloader loader = new fxmlloader();
            loader.setlocation(getclass().getresource("popup.fxml"));
            // initializing the controller
            popupcontroller popupcontroller = new popupcontroller();
            loader.setcontroller(popupcontroller);
            parent layout;
            try {
                layout = loader.load();
                scene scene = new scene(layout);
                // this is the popup stage
                stage popupstage = new stage();
                // giving the popup controller access to the popup stage (to allow the controller to close the stage) 
                popupcontroller.setstage(popupstage);
                if(this.main!=null) {
                    popupstage.initowner(main.getprimarystage());
                }
                popupstage.initmodality(modality.window_modal);
                popupstage.setscene(scene);
                popupstage.showandwait();
            } catch (ioexception e) {
                e.printstacktrace();
            }
            return popupcontroller.getresult();
        }
    }

popupcontroller.java

import java.net.url;
import java.util.hashmap;
import java.util.resourcebundle;

import javafx.fxml.fxml;
import javafx.fxml.initializable;
import javafx.scene.control.button;
import javafx.scene.control.passwordfield;
import javafx.scene.control.textfield;
import javafx.stage.stage;

public class popupcontroller extends abstractcontroller implements initializable {

    @fxml private textfield usernametf;
    @fxml private passwordfield passwordpf;
    @fxml private button connectbtn;
    private stage stage = null;
    private hashmap<string, object> result = new hashmap<string, object>();

    @override
    public void initialize(url url, resourcebundle rb) {
        connectbtn.setonaction((event)->{
            result.clear();
            result.put("username", usernametf.gettext());
            result.put("password", passwordpf.gettext());
            closestage();
        });

    }

    public hashmap<string, object> getresult() {
        return this.result;
    }

    /**
     * setting the stage of this view
     * @param stage
     */
    public void setstage(stage stage) {
        this.stage = stage;
    }

    /**
     * closes the stage of this view
     */
    private void closestage() {
        if(stage!=null) {
            stage.close();
        }
    }

}
By tvdpol on March 9 2022

Answers related to “javafx new custom pop out window”

Only authorized users can answer the Search term. Please sign in first, or register a free account.