2017-01-24 10 views
0

Заранее спасибо за поддержку ... Я кодируюсь в JavaFx, используя класс TreeView и CheckBoxTreeItem. Я хочу показать в древовидной структуре CheckBoxTreeItem (File) только имя Path или File и все sub, этот выбор пользователя. Все, что касается выбора пути, flush trow отлично работает, но когда я загружаю его в treeview, объект показывает полный путь к файлу, а не имя. Я хочу только показать имя.javafx CheckBoxTreeItem <File> TreeView <File>. Как показать только имя файла, а не полный путь?

enter image description here Для этого я использую класс, расширить CheckBoxTreeItem:

public class FilePathTreeItem_analisi extends CheckBoxTreeItem<File> 

Мой вопрос заключается в следующем: Когда я добавляю это в TreeView таким образом:

TreeView<File> treview_Base; 
treview_Base.setCellFactory(CheckBoxTreeCell.<File>forTreeView()); 
FilePathTreeItem_analisi Ckaggiunto = new FilePathTreeItem_analisi(file.toPath()); 

.. и другая команда, которая правильно загружает файл

Почему его показывают полный путь, а не только имя?

Это класс CheckBoxTreeItem:

public class FilePathTreeItem_analisi extends CheckBoxTreeItem<File> 
public FilePathTreeItem_analisi(Path file){ 
super(file.toFile()); 
dilavoro =file; 

this.fullPath=file.toString(); 
this.setIndependent(false); 

//test if this is a directory and set the icon 
if(Files.isDirectory(file)){ 
    this.isDirectory=true; 
    this.setGraphic(new ImageView(folderCollapseImage)); 
}else{ 
    this.isDirectory=false; 
    this.setGraphic(new ImageView(fileImage)); 
} 

this.setValue(file.toFile()); 

... и некоторые listyener и EventHandler ...

Так что мой вопрос: что я должен использовать в классе, которые простираются CheckBoxTreeItem на показать в TreeView имя файла, а не весь путь?

ответ

0

Использование

StringConverter<File> converter = new StringConverter<File>() { 

    @Override 
    public String toString(File file) { 
     return file.getName(); 
    } 

    @Override 
    public File fromString(String string) { 
     // not used by CheckBoxTreeCell: 
     return null ; 
    } 
}; 

treview_Base.setCellFactory(tv -> { 
    CheckBoxTreeCell<File> cell = new CheckBoxTreeCell<>(); 
    cell.setConverter(converter); 
    return cell ; 
}); 

вместо

treview_Base.setCellFactory(CheckBoxTreeCell.<File>forTreeView());