我想將我的FXML文件中定義的TreeView加載到我的java代碼中的TreeView變量中。 但總是當我嘗試它時,TreeView是NULL。JavaFX TreeView爲null爲什麼?
我到處看着Google,這裏有很多問題和答案,但沒有任何結果。
我需要TreeView動態添加TreeItems到它。
這裏放在src/rn.IconTool.view我IconOverview.FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import rn.IconTool.model.*?>
<?import javafx.scene.control.TreeView?>
<AnchorPane prefHeight="584.0" prefWidth="966.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="rn.IconTool.model.IconOverviewControllerClass">
<children>
<SplitPane dividerPositions="0.229253112033195" layoutX="2.0" layoutY="34.0" prefHeight="547.0" prefWidth="966.0">
<items>
<AnchorPane fx:id="splitPaneMenu" minHeight="0.0" minWidth="0.0" prefHeight="545.0" prefWidth="353.0">
<children>
<TreeView fx:id="treeview" layoutX="-5.0" onMouseClicked="#ShowContextMenu" prefHeight="545.0" prefWidth="218.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane prefHeight="533.0" prefWidth="675.0">
<children>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="545.0" prefWidth="702.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ImageView fitHeight="95.0" fitWidth="104.0" layoutX="22.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
<ToolBar layoutY="-1.0" prefHeight="30.0" prefWidth="968.0" />
</children>
</AnchorPane>
我RootLayout.fxml,位於.view太
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import rn.IconTool.model.*?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="rn.IconTool.model.NewCategoryClass">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" onAction="#NewtCategory" text="New Category" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
的CreateNewCategoryScreen.fxml英寸鑑於太
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import rn.IconTool.model.*?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="rn.IconTool.model.NewCategoryClass">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" onAction="#NewtCategory" text="New Category" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
而且我ContextMenuPane.fxml雖然地處.view:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="254.0" prefWidth="166.0" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8">
<!-- TODO Add Nodes -->
</AnchorPane>
這裏的控制器類,我試圖讓TreeView控件的src/rn.IconTool.model IconOverviewControllerClass:
package rn.IconTool.model;
import java.io.IOException;
import rn.IconTool.MainApp;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class IconOverviewControllerClass {
@FXML TreeView treeview;
@FXML private AnchorPane splitPaneMenu;
public IconOverviewControllerClass(){
}
@FXML
public void ShowContextMenu(MouseEvent r){
GetInTouchWithContextMenu git = new GetInTouchWithContextMenu();
// treeview = (TreeView<String>) getScene().lookup("#treeview");
if(r.getButton() == r.getButton().SECONDARY){
System.out.println("Rechtsklick");
ContextMenu contextM = git.getTheContextMenu();
System.out.println(contextM.toString());
// treeview.setContextMenu(contextM);
//
// contextM.show(treeview, r.getX(), r.getY());
// System.out.println(contextM.isShowing());
System.out.println("X: " + r.getX() + " Y: " + r.getY());
}
}
public TreeView<?> getTreeView() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/IconOverview.fxml"));
AnchorPane iconOverview = (AnchorPane) loader.load();
treeview = (TreeView<?>) iconOverview.lookup("#treeview");
System.out.println("TreeIconOver: " + treeview + " iconOverView: " + iconOverview);
return treeview;
}
}
我的類來創建一個ContextMenu什麼isn't工作,但沒有更糟。 SRC/rn.IconTool.model GetInTouchWithContextMenu:
package rn.IconTool.model;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.stage.WindowEvent;
public class GetInTouchWithContextMenu {
private ContextMenu contextM = null;
public GetInTouchWithContextMenu(){
getContextMenu();
}
private void getContextMenu(){
contextM = new ContextMenu();
contextM.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing");
}
});
contextM.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown");
}
});
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
contextM.getItems().addAll(item1);
}
public ContextMenu getTheContextMenu(){
return contextM;
}
}
這裏是我的屏幕爲我的TreeView的src /新建TreeItems rn.IconTool.model NewCategoryClass
package rn.IconTool.model;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import rn.IconTool.MainApp;
public class NewCategoryClass {
private AnchorPane categoryPane;
private Stage stage;
private Scene scene;
private Node rootIcon;
@FXML private javafx.scene.control.Button closeButton;
@FXML private javafx.scene.control.TextField categoryName;
@FXML private javafx.scene.control.TextField categoryIconName;
@FXML private javafx.scene.control.TreeView<String> treeview;
public NewCategoryClass(){
//System.out.println("Neue Kategorie erstellen.");
}
@FXML
private void NewtCategory() throws IOException{
System.out.println("Neue Kategorie angelegt.");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/CreateNewCategoryScreen.fxml"));
categoryPane = (AnchorPane) loader.load();
scene = new Scene(categoryPane);
closeButton = (Button) scene.lookup("#closeButton");
stage = new Stage();
stage.setTitle("Set New Category");
stage.setScene(scene);
stage.show();
}
@FXML
private void FinishButtonlistener() throws IOException{
// get a handle to the stage
Stage stage = (Stage) closeButton.getScene().getWindow();
// do what you have to do
System.out.println("Textfeld: " + categoryName.getText());
// leeres Textfeld = leerer String
TreeItem<String> rootItem;
if(categoryName.getText().equals("")){
}else{
if(categoryIconName.getText().equals("")){
rootItem = new TreeItem<String> (categoryName.getText());
}else{
setRootIcon(categoryIconName.getText());
rootItem = new TreeItem<String> (categoryName.getText(), getRootIcon());
}
IconOverviewControllerClass iocc = new IconOverviewControllerClass();
iocc.getTreeView();
// treeview.setRoot(rootItem);
//System.out.println("AnchorPane: " + splitPaneMenu);
System.out.println("Treeview: " + treeview);
System.out.println("TreeItem: " + rootItem);
stage.close();
}
}
private void setRootIcon(String iconName){
rootIcon = new ImageView(new Image(this.getClass().getResourceAsStream(iconName)));
}
public Node getRootIcon(){
return rootIcon;
}
public Stage getStage(){
return stage;
}
public Scene getScene(){
return scene;
}
}
我主要放在src/rn.IconTool
package rn.IconTool;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private AnchorPane iconOverview;
private Scene scene;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Icon Place");
initRootLayout();
showIconOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showIconOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/IconOverview.fxml"));
iconOverview = (AnchorPane) loader.load();
rootLayout.setCenter(iconOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
public Scene getScene(){
return scene;
}
}
我希望你們有人能幫助我
如果你需要其他類來構建完整的程序,然後問,但我希望你有一些想法,樹視圖不爲空;)
但是,在這種情況下,TreeView的是NULL太 – earny1989 2014-10-28 00:23:02
你能不能把所有的來源嗎? – user2459733 2014-10-28 07:54:31
完成了,現在一切都結束了 – earny1989 2014-10-28 14:20:17