javafx - Scene change vs Pane change -


i'm relatively new java , espacially javafx. i'm trying make menu, switches displayed content on buttonclick. i've done clearing pane , asigning new fxml-file it. 1 method controller:

protected void customstart(actionevent event) {          content.getchildren().clear();         try {              content.getchildren().add(                     (node) fxmlloader.load(getclass().getresource(                             "/view/customstartstructure.fxml")));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } 

it works fine far wuld to changing scenes well.

i want initiate scenes whit fxml-file in constructor. works within method. if try initiate in constructor invocationtargetexception caused runtimeexception caused stackoverflow error. if in other method, nullpointerexception when try change scene.

this constructor

public game() throws ioexception {          this.mainmenu = new scene((gridpane) fxmlloader.load(getclass()                 .getresource("/view/mainmenustructure.fxml")), 400, 400);         this.stage = new stage();         this.stage.setscene(mainmenu);      } 

this method in whicht invocation works:

public void run() throws exception {         /**          * set scenes different menus using panels          * fxml-files          */          this.mainmenu = new scene((gridpane) fxmlloader.load(getclass()                 .getresource("/view/mainmenustructure.fxml")), 400, 400);         mainmenu.getstylesheets().add(                 getclass().getresource("/view/mainmenudesign.css")                         .toexternalform());         this.singleracemenu = new scene((gridpane) fxmlloader.load(getclass()                 .getresource("/view/customstartstructure.fxml")), 400, 400);         /** giving stage scene */         this.setstage(new stage());         this.stage.setscene(mainmenu);         this.stage.show(); } 

this buttoncontroller:

protected void customstart(actionevent event) {         this.getstage().setscene(getsingleracemenu());      } 

i hope can give me advice!

here simple example has two fxml files, both loaded separate scenes , scenes set same stage.

controller defined scene1.fxml, since basic example of how can change scene using button event on controller.

the important part in example see how fetch current stage reference using button reference, part of scene graph :

((stage)button.getscene().getwindow()) 

if want learn how switch scenes, , go previous scene can go implement following example, loading fxml's in respective scene :

loading new fxml in same scene

example

scene1.fxml

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.layout.vbox?>  <vbox alignment="center" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" spacing="10.0" style="-fx-background-color: goldenrod;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller">     <children>         <label text="scene 1" />         <button fx:id="button" mnemonicparsing="false" onaction="#changescene" text="change scene" />     </children> </vbox> 

scene2.fxml

<?import javafx.scene.layout.vbox?>  <vbox alignment="center" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" spacing="10.0" style="-fx-background-color: cyan;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">     <children>         <label text="you have switched scene 2" />     </children> </vbox> 

scene1 controller

import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.scene.control.button; import javafx.stage.stage;  import java.io.ioexception;  public class controller {      @fxml     private button button;      @fxml     public void initialize() {     }      @fxml     private void changescene(actionevent event) {         try {             fxmlloader loader = new fxmlloader(getclass().getresource("/scene2.fxml"));             parent parent = loader.load();             ((stage)button.getscene().getwindow()).setscene(new scene(parent, 200, 200));         } catch (ioexception eox) {             eox.printstacktrace();         }     } } 

main

import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.layout.vbox; import javafx.stage.stage;  public class main extends application {      @override     public void start(stage primarystage) {         try {             fxmlloader fxmlloader = new fxmlloader(main.class.getresource("/scene1.fxml"));             vbox root = fxmlloader.load();             scene scene = new scene(root, 200, 200);             primarystage.setscene(scene);             primarystage.show();         } catch (exception e) {             e.printstacktrace();         }     }      public static void main(string[] args) {         launch(args);     } } 

output

enter image description here

enter image description here


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -