3

私はかなり単純なことをしようとしています。テーブルの特定の行の列にアイコンを配置したいと考えています。フォルダの場合は、フォルダ アイコンを表示します。ファイルの場合は、ファイルのアイコンを表示します。

JavaFX 2でこれを行う方法を知っている人はいますか?

私は非常に多くのことを試しましたが、これは非常に単純であるか、少なくともどこかの例のようです。

4

2 に答える 2

6

さて、私は巨大なダミーの瞬間を過ごしました。画像の URL パスが間違っていたことがわかりました。

テーブルに要素を追加するための優れた例を提供するサイトを見つけました。これにより、すべてを理解することができました。

以前に試した 4 つの異なる方法が機能したかどうかはわかりません。画像の URL パスが間違っていたからです。とにかく、ここにリンクとコードスニペットがあります。

要点は、 と が必要だということでしCellValueFactoryCellFactory。またはのいずれかを使用しようとしていました。updateItemTableCellのテンプレート メソッドは、 から派生した値に依存していますCellValueFactory

http://blog.ngopal.com.np/2011/10/01/tableview-cell-modifiy-in-javafx/

       TableColumn albumArt = new TableColumn("Album Art");
    albumArt.setCellValueFactory(new PropertyValueFactory("album"));
    albumArt.setPrefWidth(200); 


    // SETTING THE CELL FACTORY FOR THE ALBUM ART                 
albumArt.setCellFactory(new Callback<TableColumn<Music,Album>,TableCell<Music,Album>>(){        
    @Override
    public TableCell<Music, Album> call(TableColumn<Music, Album> param) {                
        TableCell<Music, Album> cell = new TableCell<Music, Album>(){
            @Override
            public void updateItem(Album item, boolean empty) {                        
                if(item!=null){                            
                    HBox box= new HBox();
                    box.setSpacing(10) ;
                    VBox vbox = new VBox();
                    vbox.getChildren().add(new Label(item.getArtist()));
                    vbox.getChildren().add(new Label(item.getAlbum())); 

                    ImageView imageview = new ImageView();
                    imageview.setFitHeight(50);
                    imageview.setFitWidth(50);
                    imageview.setImage(new Image(MusicTable.class.getResource("img").toString()+"/"+item.getFilename())); 

                    box.getChildren().addAll(imageview,vbox); 
                    //SETTING ALL THE GRAPHICS COMPONENT FOR CELL
                    setGraphic(box);
                }
            }
        };
        System.out.println(cell.getIndex());               
        return cell;
    }

});
于 2013-05-02T16:06:44.520 に答える
3

提供された回答がうまくいかなかった場合(私にとってはうまくいかなかったように)、これが私が見つけた解決策でした(もちろん、tableViewを作成して列を追加する必要があります):

//Create your column that will hold the image    
private final TreeTableColumn<YourObjectClass,ImageView> columnImage= new TreeTableColumn<YourObjectClass,ImageView>("Image");

public void start() {
    //Set your cellValueFactory to a SimpleObjectProperty
    //Provided that your class has a method "getImage()" this will work beautifully!            
    columnImage.setCellValueFactory(c-> new SimpleObjectProperty<ImageView>(new ImageView(c.getValue().getValue().getImage())));
}
于 2015-07-15T22:11:00.143 に答える