0

I am trying to build a text editor for my first program project. I have the treeview on the left side and the tabpane on the right. There is a textarea on the tab. My intent is to display the contents of the file that is selected in the treeview, if it is a file, to the textarea. Sounds simple enough right?
I have been trying all day to get this to work and I can not figure it out. Something is wrong and it is beyond my skill level. Any advice is appreciated. Thank you to all you help make this a fantastic educational platform!

import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Tab
import javafx.scene.control.TabPane
import javafx.scene.control.TextArea
import javafx.scene.control.TreeItem
import javafx.scene.control.TreeView
import javafx.scene.control.cell.TextFieldTreeCell
import javafx.stage.FileChooser
import javafx.stage.Window

class Controller implements Initializable{

@FXML
TextArea inputSpace
@FXML
Tab tabName
@FXML
TabPane tabPane
@FXML
TreeView fileTree

void openAndSetTab(File file) throws FileNotFoundException{
    if(file.canRead()){
        def openFile = new Tab()
        tabPane.getTabs().add(openFile)

        //set file name to tab title
        def dotPosition = file.name.indexOf('.')
        def fileName = file.name.substring(0,dotPosition)
        openFile.text = fileName

        //add file data to text area
        def inputArea = new TextArea()
        openFile.setContent(inputArea)
        def data = file?.getText()
        inputArea.setText(data)

        //bring to front
        openFile.getTabPane().getSelectionModel().select(openFile)
    }else{
        println('Cannot read...')
        println(file.absolutePath)
    }
}

void openFile() {
    //get file
    def chooser = new FileChooser()
    def file = chooser.showOpenDialog(Window.getResource('Main.java') as Window)

    openAndSetTab(file)
}

/**
 * save text data to file
 * and set title of tab
 */
void saveFile() {
    FileChooser chooser = new FileChooser()
    def data = inputSpace.text
    def file = chooser.showSaveDialog(Window.getResource('Main.java') as Window)
    if(data != null){
        file.text = ''
        file << data
    }

    //set tab title to saved file name
    int dotPosition = file.name.indexOf('.')
    def fileName = file.name.substring(0,dotPosition)
    tabName.text = fileName
}

/**
 * stackoverflow.com Answer
 * https://stackoverflow.com/questions/38278601/javafx-treeview-directory-listing
 * answered Jul 9 '16 at 6:46
 * Author: fabian
 * Original was using CheckBoxTreeItem, I updated code to my needs
 * @param inputDirectoryLocation
 */
void displayTreeView(String inputDirectoryLocation) {
    def tree = new GetTree()

    // Creates the root item.
    TreeItem<String> rootItem = new TreeItem<>(inputDirectoryLocation)

    // Hides the root item of the tree view.
    fileTree.setShowRoot(false)

    // Creates the cell factory.
    fileTree.setCellFactory(TextFieldTreeCell.forTreeView())

    // Get a list of files.
    def fileInputDirectoryLocation = new File(inputDirectoryLocation)
    List<File> fileList = fileInputDirectoryLocation.listFiles()

    // create tree
    for (File file : fileList) {
        tree.createTree(file, rootItem)
    }

    fileTree.setRoot(rootItem)
}

@Override
void initialize(URL url, ResourceBundle resourceBundle) {
    displayTreeView(System.getProperty('user.dir'))
    fileTree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
        @Override
        void changed(ObservableValue<? extends TreeItem> observableValue, TreeItem treeItem, TreeItem t1) {
            def file = new File(t1.value.toString())
            if(file.isFile()){
                openAndSetTab(file)
            }
        }
    })
  }
}

I added the groovy jar to the classpath so that's why it looks wired. It't the superset of java. Love using it. What am I doing wrong?

The GetTree class is as follows,

import javafx.scene.control.TreeItem

 /**
 * stackoverflow.com Answer
 * https://stackoverflow.com/questions/38278601/javafx-treeview-directory-listing
 * answered Jul 9 '16 at 6:46
 * Author: fabian
 * Original was using CheckBoxTreeItem, I updated code to my needs
 */
 class GetTree {
 static void createTree(File file, TreeItem<String> parent) {
    if (file.isDirectory()) {
        TreeItem<String> treeItem = new TreeItem<>(file.getName())
        parent.getChildren().add(treeItem)
        for (File f : file.listFiles()) {
            createTree(f, treeItem)
        }
    } else{//must be a file
        parent.getChildren().add(new TreeItem<>(file.getName()))
    }
  }

}

Projects folder is

  • tetragrammaton

    • .idea

      -sub-directories.here

    • out

      -sub-directories.here

    • src

      • sub-directories.here

    testing.txt

    TetraGrammaton.iml

The only files that open in the textarea that is on the tab, is the 2 files, testing.txt and TetraGrammaton.iml. The other directories open upon clicking but do not open in the textarea.

  • 3
    "Something is wrong" is not enough information. Also make your code [mcve] please. – c0der Oct 28 '18 at 04:33
  • 1
    The `GetTree` class is not posted but it may contain your error. On the other hand it's hard to tell, if this is an issue, since you don't describe the issue in a way that helps narrow down the issue (we only know that there is an issue; there is no description of the expected and actual results). Please [edit] the question and improve it. – fabian Oct 28 '18 at 09:58

0 Answers0