JavaFX TreeView为null为什么?

时间:2014-10-26 23:05:18

标签: java treeview fxml

我想将我的FXML文件中定义的TreeView加载到我的java代码中的TreeView变量中。 但总是当我尝试它时,TreeView是NULL。

我到处寻找谷歌,这里有很多问题和答案,但没有成功。

我需要TreeView动态地添加TreeItems。

这里我的IconOverview.FXML文件位于src / rn.IconTool.view:

     <?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>

.view中的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>

这里是Controller类,我试图获取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什么不起作用,但没有那么糟糕。 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 / rn.IconTool.model创建新TreeItems的屏幕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;)

2 个答案:

答案 0 :(得分:0)

您不必查看树视图: treeview =(TreeView)iconOverview.lookup(&#34;#treeview&#34;);

您可以使用fxml文件中定义的fx:id和控制器中的注释@FXML自动检索treeView。

答案 1 :(得分:0)

我知道为什么我的TreeView为NULL。我忘了实现javafx.fxml.Initializable并添加未实现的方法。

这是我的新IconOverviewControllerClass,不用担心静态TreeView仅用于测试目的;)

     package rn.IconTool.model;

 import java.io.IOException;
 import java.net.URL;
 import java.util.ResourceBundle;

 import javafx.fxml.FXML;
 import javafx.scene.control.ContextMenu;
 import javafx.scene.control.TreeView;
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.AnchorPane;



 public class IconOverviewControllerClass implements javafx.fxml.Initializable{



 @FXML public TreeView<String> treeview;
 @FXML private AnchorPane splitPaneMenu;

 private static TreeView<String> statictreeView;

 public IconOverviewControllerClass(){

}



 @FXML
 public void initMyComponents(MouseEvent r) throws IOException{
     if(getTreeView() == null){
         System.out.println(treeview);
         statictreeView = treeview;
         System.out.println("static init: " + statictreeView);
     }


     ShowContextMenu(r);
 }


    public void ShowContextMenu(MouseEvent r){

        if(r.getButton() == r.getButton().SECONDARY){
            System.out.println("Rechtsklick");
            GetInTouchWithContextMenu git = new GetInTouchWithContextMenu();
            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<String> getTreeView() throws IOException{
        return statictreeView;
    }



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //System.out.println(treeview);
        statictreeView = treeview;
    }
 }