0

This is a menubar with two items. Under "Chapter" I would like to be able to select a chapter from the MenuItems and have it load a new .fxml file.

I have produced a cut-down version that gives a System.out.println result when a MenuItem is selected. On the "Chapter" it doesn't load the MenuItem selected.

I have copied my main, fxml and controller files together with the output listing at run time.

Main is JavaFXMenulItem.java

package javafxmenuitem;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXMenuItem extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLMenuItem.fxml"));        
        Scene scene = new Scene(root);       
        stage.setScene(scene);
        stage.show();
    }

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

FXML file is FXMLMenulItem.fxml

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

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

<AnchorPane id="AnchorPane" fx:id="root" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxmenuitem.FXMLMenuItemController">
   <children>
      <MenuBar layoutY="1.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="miclose" mnemonicParsing="false" onAction="#handleButtonAction" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Chapter">
            <items>
              <MenuItem fx:id="michapone" mnemonicParsing="false" onAction="#handleButtonAction" text="Ch.1  Introduction" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

Controller is FXMLMenulItemController.java

package javafxmenuitem;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

public class FXMLMenuItemController implements Initializable {   
    @FXML
    private MenuItem miclose;
    @FXML
    private MenuItem michapone;
    @FXML
    private Parent root;    
    Stage stage;

    @FXML
    private void handleButtonAction(ActionEvent e) throws IOException {      
        if(e.getSource()==miclose){
            System.out.println("this is Close");
            }    
        else if(e.getSource()==michapone){
            System.out.println("this is chap 1");  

            //get reference to the button's stage
            stage = (Stage) root.getScene().getWindow();
            //load up OTHER FXML document
            root = FXMLLoader.load(getClass().getResource("FXMLMenuItem1.fxml"));

            //create a new scene with root and set the stage
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
            }      
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }       
}

Output listing is

ant -f C:\\Users\\john\\Documents\\NetBeansProjects\\JavaFXMenuItem jfxsa-run
init:
Deleting: C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\built-jar.properties
Compiling 2 source files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\classes
Copying 2 files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\classes
compile:
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_31\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_31\jre\..\lib\ant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
Copying 12 files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306
jfx-project-run:
Executing C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306\JavaFXMenuItem.jar using platform C:\Program Files\Java\jdk1.8.0_31\jre/bin/java
this is chap 1
Deleting directory C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306
jfxsa-run:
BUILD SUCCESSFUL (total time: 18 seconds)

The second .fxml file FXMLMenuItem1.fxml (just the AnchorPane etc):

<AnchorPane id="AnchorPane" fx:id="root" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxmenuitem.FXMLMenuItemController">
   <children>
      <MenuBar layoutY="1.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="miclose" mnemonicParsing="false" onAction="#handleButtonAction" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Chapter">
            <items>
              <MenuItem fx:id="michapone" mnemonicParsing="false" onAction="#handleButtonAction" text="Ch.1  Introduction" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

Any help would be appreciated

  • Is this a code review request or do you have a specific question? – jewelsea Apr 13 '15 at 18:06
  • Can you add the other FXML file (or a trimmed-down version of it); that way we can see the issue. There is nothing obviously wrong with the code you have posted. – James_D Apr 13 '15 at 18:09
  • I had an earlier request and was asked to produce a cut-down version to focus in on how to load a new .fxml file from a menuItem. I don't get errors at run time and it prints the System.out BUT doesn't load in the new file. –  Apr 13 '15 at 18:13
  • Having been asked to produce all my code - my question is why isn't it working? –  Apr 13 '15 at 18:14
  • It works fine for me if I create a very simple `FXMLMenuItem1.fxml` just displaying a `Label`. So I suspect the problem is in that file (or related to it). What happens if you just try to load that directly, by changing the fxml file in your `start()` method? – James_D Apr 13 '15 at 18:16
  • Works just fine for me... Loads the "new" fxml (which is of course the same as the original). – James_D Apr 13 '15 at 18:27
  • James thanks again. I was loading the SAME file on top of the existing. I put a simple label into the new .fxml and its working. Simple, and once again many thanks. –  Apr 13 '15 at 18:29

0 Answers0