我正在尝试为我的第一个程序项目构建一个文本编辑器。我在左侧有treeview,在右侧有tabpane。选项卡上有一个文本区域。我的意图是将在树形视图中选择的文件的内容(如果是文件)显示到文本区域。听起来足够简单吧?
我一直在努力使它正常工作,我无法弄清楚。出了点问题,这超出了我的技能水平。
任何建议表示赞赏。谢谢大家的帮助,使它成为一个了不起的教育平台!
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)
}
}
})
}
}
我将groovy jar添加到类路径中,这就是为什么它看起来是有线的。它不是Java的超集。喜欢使用它。我在做什么错了?
GetTree类如下,
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()))
}
}
}
“项目”文件夹为
四卦法
.idea
-sub-directories.here
退出
-sub-directories.here
src
testing.txt
TetraGrammaton.iml
在选项卡上的文本区域中打开的唯一文件是2个文件testing.txt和TetraGrammaton.iml。其他目录在单击时打开,但不在文本区域中打开。