我试图弄清楚为什么父元素在以下JavaFX代码中总是null
。
MenuBar
用作gl.fmxl中的子项(参见上文)。 MenuBar
正在使用名为MenuController的控制器。现在当我点击菜单点击MenuController
时,@FXML protected VBox gl;
中的null
总是GlController
以下是使用下面的menu_bar.fxml文件创建MenuBar组件的方法
public class MenuBar extends FlowPane {
public MenuBar(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fragments/top_menu.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(new MenuController());
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
gl.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import com.ui.layouts.MenuBar?>
<?import javafx.scene.control.Button?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.ui.controllers.GlController">
<VBox fx:id="gl">
<MenuBar fx:id="menuBar"></MenuBar>
</VBox>
</AnchorPane>
top_menu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.FlowPane?>
<fx:root type="javafx.scene.layout.FlowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" xmlns:fx="http://javafx.com/fxml">
<children>
<MenuBar >
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="createCsvFile" mnemonicParsing="false" onAction="#exportCsvData" text="GL" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="CSV">
<items>
<MenuItem fx:id="openCsvFile" mnemonicParsing="false" onAction="#processCsvData" text="open" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Products">
<items>
<MenuItem mnemonicParsing="false" onAction="#showProducts" text="saving" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Accounts">
<items>
<MenuItem mnemonicParsing="false" onAction="#showAccountTransactions" text="show" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</fx:root>
Main.java
以下是我初始化javaFX应用程序的方法
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader();
Parent parentView = fxmlLoader.load(getClass().getResource("ui/layouts/gl.fxml"));
primaryStage.setScene(new Scene(parentView, 1200, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MenuController.java
public class MenuController {
GlController glController = new GlController();
@FXML protected void processCsvData(ActionEvent event) throws IOException {
MenuItem menuItem = (MenuItem)event.getSource();
if (menuItem.getId().equals(openCsvFile.getId())) {
File file = browseFile("CSV", "*.csv");
String csvData = buildCsv(file.getPath()).toString();
List<Transactionable> transactionList = glToCsv.buildTransactions(csvData, TransactionType.ACTUAL_TRANSACTION);
addTableItem(transactionTableView, transactionList);
}
}
}
GlController
public class GlController {
@FXML protected VBox gl;
public Scene getScene(){
return gl.getScene();
}
}
点击菜单调试processCsvData
的值glController.getScene()
null