简体   繁体   中英

I can not read Tabpane using FXML Loader

I registered in Ukraine, so sorry for mistakes. I want to set a basis for FXML file Tabpane but an exception comes out when I do so:

 public Stage stage;
 private Scene scene;
 public void start(Stage stage) {
    try {
        q=(TabPane)FXMLLoader.load(Main.class.getResource("test.fxml"));
    } catch(Exception e) {
        e.printStackTrace();
    }
    scene=new Scene(q,640,480);
    stage.setScene(scene);
    stage.show();
}

But when I want to set a basis for AnchorPane program, there is no error when writing:

public void start(Stage stage) {
    try {
        pane=(AnchorPane)FXMLLoader.load(Main.class.getResource("test.fxml"));
    } catch(Exception e) {
        e.printStackTrace();
    }
    scene=new Scene(pane,640,480);
    stage.setScene(scene);
    stage.show();
}

FXML file here https://drive.google.com/open?id=0B5LBeiES5-shWDFLRnBPZUhhdmM

excerpt from the FXML file:

<TabPane fx:id="q" prefHeight="480.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <tabs>
      <Tab fx:id="q0">
         <content>
            <BorderPane prefHeight="546.0" prefWidth="752.0">
               <top>
                  <MenuBar prefHeight="25.0" prefWidth="432.0" BorderPane.alignment="CENTER">
                     <menus>
                        <Menu mnemonicParsing="false" text="File">
                          <items>
                              <MenuItem mnemonicParsing="false" onAction="#exitfile" text="Close" />
                           </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Options">
                           <items>
                              <Menu mnemonicParsing="false" text="Language">
                                 <items>
                                    <RadioMenuItem mnemonicParsing="false" text="EN" />
                                    <RadioMenuItem mnemonicParsing="false" text="UA" />
                                 </items>
                              </Menu>
                              <MenuItem mnemonicParsing="false" text="Others" />
                           </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Help">
                           <items>
                              <MenuItem mnemonicParsing="false" text="About" />
                           </items>
                    </Menu>
                    </menus>
                  </MenuBar>
               </top>
               <center>
                  <Pane prefHeight="501.0" prefWidth="735.0" BorderPane.alignment="CENTER">
                     <children>
                        <ImageView fitHeight="520.0" fitWidth="640.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../Test/res/fon.jpg" />
                           </image>
                        </ImageView>
                        <GridPane layoutX="216.0" layoutY="145.0" prefHeight="98.0" prefWidth="248.0">
                           <columnConstraints>
                              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                           </columnConstraints>
                           <rowConstraints>
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />    
                           </rowConstraints>
                           <children>
                              <Button fx:id="b1" disable="true" mnemonicParsing="false" onAction="#enblean" prefHeight="85.0" prefWidth="248.0"  text="Розпочати тест" GridPane.rowIndex="3" />
                             <Label alignment="CENTER" prefHeight="45.0" prefWidth="243.0" text="Введіть ім'я та прізвище">
                                 <font>
                                    <Font name="Lucida Fax Regular" size="14.0" />
                                 </font>
                              </Label>
                              <TextField fx:id="im" onAction="#b1dis"     onKeyPressed="#b1dis" onKeyReleased="#b1dis" onKeyTyped="#b1dis"  prefHeight="29.0" prefWidth="248.0" GridPane.rowIndex="1" />
                           </children>
                        </GridPane>
                     </children>
                  </Pane>
               </center>
            </BorderPane>
         </content>
      </Tab>    

passage end of FXML file

   </children>
                       </Pane>
                     </center>
                  </BorderPane>
               </children>
            </AnchorPane>
         </content>
      </Tab>
   </tabs>
</TabPane>

exeption:

javafx.fxml.LoadException: No controller specified.  
/C:/Users/Admin/workspace/Testp/bin/ua/NazarRepyanskiy/test.fxml:34  
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)  
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)  
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)  
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599 )  
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)  
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)  
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)  
at ua.NazarRepyanskiy.Main.start(Main.java:236)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)  
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)  
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)  
at java.security.AccessController.doPrivileged(Native Method)  
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)  
at  com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)  
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)  
at   com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)  
at java.lang.Thread.run(Unknown Source)  
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
at java.lang.reflect.Method.invoke(Unknown Source)  
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)  
at  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)   
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
at java.lang.reflect.Method.invoke(Unknown Source)  
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)  
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)  
at java.lang.Thread.run(Unknown Source)  
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)  
at javafx.scene.Scene.<init>(Scene.java:223)  
at ua.NazarRepyanskiy.Main.start(Main.java:240)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)  
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)  
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)  
at java.security.AccessController.doPrivileged(Native Method)  
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)  
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)  
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)  
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)  
... 1 more  
Exception running application ua.NazarRepyanskiy.Main  

You cannot use the onAction attribute with a value of #exitfile or any other values starting with # , unless you specify a controller either using the fx:controller attribute in the root element of the fxml file

<TabPane fx:controller="mypackage.ControllerClass"
         fx:id="q" prefHeight="480.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">

or by specifying a controller instance using FXMLLoader.setController on a controller instance you use to load the fxml file:

FXMLLoader loader = new FXMLLoader(Main.class.getResource("test.fxml"));
loader.setController(controllerInstance);
q=(TabPane)loader.load();

(the fx:controller attribute must not be specified in the fxml file for this approach to work)


BTW: you shouldn't attempt to continue after getting an exception you cannot recover from. In this case this leads to 2 stacktraces being printed: One for the exception when loading the fxml file and one that notifies you that you cannot continue and use q as root of a Scene after

q=(TabPane)FXMLLoader.load(Main.class.getResource("test.fxml"));

did not assign a non- null value to q . An better idea would be throwing a exception with the original exception as root or simply adding a throws clause to the start method.

try {
    ...
} catch(Exception e) {
    throw new IllegalStateException(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM