javafx - Unable to Call another fxml page on button click event -


i trying come simple tool want call fxml page on button click present fxml page. main java class designed follows:

import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.stage.stage; import javafx.scene.parent; import javafx.scene.scene; import javafx.scene.control.titledpane; import javafx.scene.layout.borderpane;   public class main extends application  {    @override   public void start(stage stage) {     try {          titledpane page = (titledpane)fxmlloader.load(getclass().getresource("newtest.fxml"));          scene scene = new scene(page);         stage.setscene(scene);             stage.settitle("welome page");             stage.show();      } catch (exception e) {         e.printstacktrace();     }  }  public static void main(string[] args) {     application.launch(main.class, (java.lang.string[])null); }} 

and controller class designed :

import javafx.event.actionevent; import java.net.url; import java.util.resourcebundle; import javafx.event.eventhandler; 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.titledpane; import javafx.stage.stage;   public class simplecontroller implements initializable  {     @fxml //  fx:id="loginbtn"    private button loginbtn; // value injected fxmlloader      @override // method called fxmlloader when                  initialization complete    public void initialize(url fxmlfilelocation, resourcebundle resources) {     assert loginbtn != null : "fx:id=\"mybutton\" not injected: check fxml file 'simple.fxml'.";      // initialize logic here: @fxml variables have been injected     loginbtn.setonaction(new eventhandler<actionevent>() {          public void handle(actionevent event) {              stage stage;               parent root;              if(event.getsource()==loginbtn){                 //get reference button's stage                          stage=(stage) loginbtn.getscene().getwindow();                 //load other fxml document                 try{                     titledpane page =(titledpane) fxmlloader.load(getclass().getresource("secondpage.fxml"));                 }catch(exception e)                 {e.printstacktrace();}               }         }     }); } } 

and fxml page like:

<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.control.passwordfield?> <?import javafx.scene.control.textarea?> <?import javafx.scene.control.titledpane?> <?import javafx.scene.layout.anchorpane?> <?import javafx.scene.text.font?>   <titledpane text="welcome vacation planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">   <content>       <anchorpane prefheight="330.0" prefwidth="497.0">          <children>             <label layoutx="14.0" layouty="52.0" prefheight="25.0"  prefwidth="189.0" text="user name">               <font>                   <font size="24.0" />               </font>              </label>              <textarea layoutx="53.0" layouty="106.0" prefheight="41.0" prefwidth="391.0" />                 <label layoutx="14.0" layouty="165.0" text="password">                  <font>                     <font size="24.0" />            </font>         </label>         <button fx:id="loginbtn" layoutx="114.0" layouty="262.0" mnemonicparsing="false" text="login">            <font>               <font size="18.0" />            </font>         </button>         <button fx:id="exitbtn" layoutx="271.0" layouty="262.0" mnemonicparsing="false" text="exit">            <font>               <font size="18.0" />            </font>            </button>            <passwordfield layoutx="53.0" layouty="207.0" prefheight="39.0" prefwidth="391.0" />          </children>         </anchorpane>        </content>       </titledpane> 

when clicking on button login not opening second xml page trying open on button click.

thanks in advance.

you load nodes , nothing it.

you need add them existing scene or replace scene:

try{     titledpane page =(titledpane) fxmlloader.load(getclass().getresource("secondpage.fxml"));     scene newscene = new scene(page);     stage.setscene(newscene); } catch(exception e) {     e.printstacktrace(); } 

furthermore haven't associated controller fxml. use e.g. fx:controller attribute qualified class name of controller class @ root of fxml this:

<titledpane text="welcome vacation planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.package.simplecontroller"> 

where my.package package containing controller.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -