Javafx分割窗格与树视图

时间:2016-08-27 08:31:42

标签: javafx

请帮我将addproject控制器中的项目添加到项目控制器。我想将addprojects控制器中的项添加到projectscontroller方法。请指导我解决这个问题。请指导动态更改右侧拆分窗格视图并在树视图中添加树项并向用户显示。

    package com.define.controller;

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;

    import java.util.ResourceBundle;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.event.Event;

    //import org.apache.commons.io.FileUtils;

    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;

    import javafx.scene.Node;
    import javafx.scene.control.CheckBox;
    import javafx.scene.control.Tab;
    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.scene.layout.HBox;

    import javafx.stage.DirectoryChooser;
    import javax.naming.Context;
    import testapp.TestApp;

    public class ProjectsController implements Initializable{

        // @FXML private HBox fileList;
        @FXML TreeView<String> Maintree ;
        @FXML private AnchorPane Anchorpane;

         File destinationFolder;



         //Image img = new Image();
         //private final Node rootIcon = new ImageView(img);
    /*  private final Node rootIcon = new ImageView(
                    new Image(getClass().getResourceAsStream("G:\\workspace\\DefineApp\\src\\images\\folder.png")));*/
         File file = new File("D:\\DefineApp\\src\\images\\folder.png");
            Image image = new Image(file.toURI().toString(),20,20,false,false);
            private  Node rootIcon = new ImageView(image);    


         @Override
            public void initialize(URL location, ResourceBundle resources) {
             File currentDir = new File("D:\\DefineApp\\src\\Documents"); // current directory
                 rootItem.setExpanded(true);
             Maintree.setShowRoot(false);
                     Maintree.setRoot(rootItem);
                     findFiles(currentDir);
                 try {
                     changeView();
                 } catch (IOException ex) {
                     Logger.getLogger(ProjectsController.class.getName()).log(Level.SEVERE, null, ex);
                 }
            }


        public void UploadFiles() throws IOException{
            DirectoryChooser directory = new DirectoryChooser();
            directory.setTitle("Choose Directory");

             final File selectedDirectory = directory.showDialog(null);
             File sourcefolder = new File(selectedDirectory.getAbsolutePath());
             String projectName = "Symbiance";
             String studyName = "study1";
             destinationFolder = new File("G:\\workspace\\DefineApp\\src\\"+projectName+"\\"+studyName);

                if(!destinationFolder.exists()){
                    destinationFolder.mkdirs();

                        if(destinationFolder.isDirectory()){
                            // FileUtils.copyDirectory(sourcefolder, destinationFolder);
                             System.out.println("Files Copied Successfully");
                        }
                }else{

                    if(destinationFolder.isDirectory()){
                     //FileUtils.copyDirectory(sourcefolder, destinationFolder);
                     System.out.println("Files Copied Successfully");
                    }

                }
                    //Method call for list the files
                listFiles();
        }

        public void listFiles(){
             //fileList.setPadding(new Insets(15, 12, 15, 12));
             //fileList.setSpacing(10);
            File[] listOfFiles = destinationFolder.listFiles();
            CheckBox[] cbs = new CheckBox[listOfFiles.length];
             int i=0;
            for (File file : listOfFiles) {
                CheckBox cbox = cbs[i]= new CheckBox(file.getName());
                i++;

            }

            //fileList.getChildren().addAll(cbs);
        }

        TreeItem<String> rootItem = new TreeItem<String>("Root");

        public void findFiles(File currentDir){

             //rootItem.setGraphic(rootIcon);;
            TreeItem<String> subfolder;
             // parent = new TreeItem<String> (parent.getValue(),rootIcon);

            System.out.println("1");
             File folder = new File(currentDir.getAbsolutePath());
                File[] listOfFiles = folder.listFiles();
                for (File file:listOfFiles) {           

                    if(file.isDirectory()){
                         subfolder = new TreeItem<String> (file.getName());
                        rootItem.getChildren().add(subfolder);
                        File subFolderName = new File(file.getAbsolutePath());
                        File[] subFolderFiles = subFolderName.listFiles();

                        for(File f:subFolderFiles){
                            TreeItem<String> it= new TreeItem<String>(f.getName());
                            subfolder.getChildren().add(it);
                        }

                    }


                }





        }

        public void changeView() throws IOException{
        System.out.println("change view is called");

                Anchorpane.getChildren().add(TestApp.getInstance().designChooser("com/define/views/AddProject.fxml"));


            //setContent();
        }

     public void addProject(String name,Event e){
         rootItem.getChildren().add(new TreeItem(name));
         System.out.println("test check");
     }

    }

another controller;

package com.define.controller;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TreeItem;
import javafx.scene.text.Text;

public class AddProjectController implements Initializable{
    @FXML Button yesbutton;
    @FXML RadioButton projectYesRadio;
    @FXML RadioButton projectNORadio;
    @FXML TextField projectId;
    @FXML TextField projectName;

    final ToggleGroup Projectgroup = new ToggleGroup();
    final File baseFolder = new File("D:\\DefineApp\\src\\Documents");
    File projectFolder;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        projectYesRadio.setToggleGroup(Projectgroup);
        projectNORadio.setToggleGroup(Projectgroup);
        projectYesRadio.setSelected(true);

    }

     public void addProject(ActionEvent e){

             projectFolder = new File("D:\\DefineApp\\src\\Documents\\"+projectName.getText());
             projectFolder.mkdir();
                         ProjectsController pt = new ProjectsController();
                         pt.addProject(projectName.getText(),e);
                          //System.out.println(projectController.Maintree.getRoot());
             //.rootItem.getChildren().add(new TreeItem(projectName.getText()));


         //projectController.findFiles(baseFolder);
         System.out.println("test check");
     }

     public void radiocalled(){
         //yesbutton.setVisible(false);
     }  
}

0 个答案:

没有答案