2014-09-30 52 views
-1

我想使我的未修饰场景可拖动。我的根布局包含带有控件和内容的工具栏。我给了工具栏的id并为.fxml布局设置了控制器。 然后我做了所有步骤,如Dragging an undecorated Stage in JavaFX 但我有空指针,当试图调用EffectUtilities.makeDraggable(stage,tool_bar); 下面是代码:JavaFX中的NPE在调用控制器中的字段时

主要

public class UpdaterMain extends Application{ 

    private Stage primaryStage; 
    private BorderPane rootLayout; 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     this.primaryStage = primaryStage; 
     this.primaryStage.initStyle(StageStyle.UNDECORATED); 
     this.primaryStage.initStyle(StageStyle.TRANSPARENT); 

     initRootLayout(); 
     showContentOverview(); 

    } 

    /** 
    * Initializes the root layout. 
    */ 
    public void initRootLayout() { 
     try { 
      // Load root layout from fxml file. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(UpdaterMain.class.getResource("RootLayout.fxml")); 
      rootLayout = (BorderPane) loader.load(); 

      // Show the scene containing the root layout. 
      Scene scene = new Scene(rootLayout); 
      primaryStage.setScene(scene); 

      UpdaterMainController controller = 
        loader.getController(); 
      controller.registerStage(primaryStage); <<--Here is NPE 

      primaryStage.show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Shows main content 
    */ 
    public void showContentOverview() { 
     try { 
      // Load person overview. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(UpdaterMain.class.getResource("updater-layout.fxml")); 
      SplitPane contentOverview = loader.load(); 

      // Set person overview into the center of root layout. 
      rootLayout.setCenter(contentOverview); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Stage getPrimaryStage() { 
     return primaryStage; 
    } 

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

控制器

import com.iminegination.utils.EffectUtilities; 
import javafx.application.Platform; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.ToolBar; 
import javafx.stage.Stage; 

public class UpdaterMainController { 
    @FXML 
    private ToolBar tool_bar; 

    public void registerStage(Stage stage) { 
     EffectUtilities.makeDraggable(stage, tool_bar); <<--Here is NPE (tool_bar is null, but why?) 
    } 

    public UpdaterMainController() { 
    } 
} 

layout.fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="690.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.iminegination.updater.view.UpdaterMainController"> 
    <top> 
     <ToolBar id="tool_bar" nodeOrientation="RIGHT_TO_LEFT" prefHeight="40.0" prefWidth="200.0"> 
     <items> 
      <Button id="exitButton" mnemonicParsing="false" text="X" onAction="#click"/> 
      <Button mnemonicParsing="false" text="Settings" /> 
      <Button mnemonicParsing="false" text="_" /> 
     </items> 
     </ToolBar> 
    </top> 
</BorderPane> 

回答

1

您需要定义一个fx:id属性为FXML您的控件。

在你FXML您有:

<ToolBar id="tool_bar" . . . 

你应该有:

<ToolBar id="tool_bar" fx:id="tool_bar" . . . 

id定义CSS ID。

fx:id定义FXML和控制器变量名称之间的链接。

相关问题