子FXML窗格中不显示JavaFX TreeView

时间:2018-02-22 21:50:48

标签: java javafx treeview scenebuilder

我是JavaFX的新手,并尝试构建一个具有root登陆阶段(rootLayout)的简单客户端应用程序,其中的按钮允许我用子FXML交换RootLayout.FXML。当我尝试在子FXML窗格中加载TreeView时,我的问题就开始了。 TreeView用于在子窗格的此区域内加载文件目录。 谢谢!!!

  

主要

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class MainApp extends Application {

    public static Properties configProp;
    private Stage primaryStage;
    private static BorderPane rootLayout;

    /**
     * Just a root getter for the controller to use
     */
    public static BorderPane getRoot() {
      return rootLayout;
    }

    @Override
    public void start(Stage primaryStage) {

        this.primaryStage = primaryStage;        
        initRootLayout(); 
    }

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

    /***
     * Initialize the root layout
     * @param args
     */
    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 scene = new Scene(rootLayout);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  

RootLayoutController

import java.io.IOException;


import einMyQueue.MainApp;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;

public class RootLayoutController {

    // Reference to the main application
    private MainApp mainApp;

    /**
     * Is called by the main application to give a reference back to itself.
     * 
     * @param mainApp
     */
    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;
    }

    /**
     * Selects the FAST Scene
     */
    @FXML
    private void handleFASTSceneSelection() {
        try {           
            SplitPane paneOne = FXMLLoader.load(getClass().getResource("FASTMessageRequest.fxml"));
            BorderPane border = MainApp.getRoot();
            border.setCenter(paneOne);
        } catch (IOException e) {
              e.printStackTrace();
        }
    }

}
  

实现TextView的子控制器

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import einMyQueue.MainApp;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.DirectoryChooser;

public class FASTMessageRequestController {
    private MainApp mainApp;

    // FXML Annotations 
    @FXML private TreeView<String> msgDirTreeView;
    @FXML private TextArea msgBodyTextArea;
    @FXML private TextArea consoleTextArea;

    public void initialize() {
        // Using a Tree View
        msgDirTreeView = new TreeView<String>();
        DirectoryChooser dirChooser = new DirectoryChooser();
        URL url = getClass().getResource("../resources/templates");
        try {
            dirChooser.setInitialDirectory(new File(url.toURI()));
            File file = dirChooser.getInitialDirectory();               
            msgDirTreeView.setRoot(getNodesForDirectory(file));

        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }

    public TreeItem<String> getNodesForDirectory(File directory) { 
        TreeItem<String> msgDirRoot = new TreeItem<String>(directory.getName());
        for(File iFile : directory.listFiles()) {
            System.out.println("Loading " + iFile.getName());
            if(iFile.isDirectory()) { 
                msgDirRoot.getChildren().add(getNodesForDirectory(iFile));
            } else {
                msgDirRoot.getChildren().add(new TreeItem<String>(iFile.getName()));
            }
        }
        return msgDirRoot;
    }

    /***
     * Name:    setMainApp
     * Purpose: Is called by the main application to give a reference back to itself.
     * 
     * @param mainApp
     */
    public void setMainApp(MainApp mainApp) {
        // Is called by the main application to give a reference back to itself.
        this.mainApp = mainApp;
    }
}

0 个答案:

没有答案