2017-01-24 3 views
0

ありがとうございます。 私はTreeViewクラスとCheckBoxTreeItemを使用してJavaFxでコーディングしています。私は、TreeViewにCheckBoxTreeItem(File)をPathまたはFileの名前とそのすべてのsub、そのユーザの選択肢だけを表示したいと思います。パスを選択することについてのすべてのものは、うまくいきなり動作しますが、私はtreeviewでこれをアップロードすると、オブジェクトはファイルのフルパスを表示し、名前は表示しません。私は名前を示すだけです。私はこのように、ツリービューにこれを追加すると::javafx CheckBoxTreeItem <File> TreeView <File>。完全なパスではなくファイルの名前だけを表示するには?

public class FilePathTreeItem_analisi extends CheckBoxTreeItem<File> 

私の質問はこれです:

enter image description here

私はCheckBoxTreeItemを拡張するクラスを使用してこれを行うには..

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を拡張するクラスで使用するために持っているものツリービューに、パス全体ではなくファイルの名前を表示しますか?

答えて

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()); 
関連する問題