将图像放入JavaFX 2表中 [英] Placing an image into a JavaFX 2 table
问题描述
我正在尝试做一些非常简单的事情。我想在表格中的特定行的列中放置一个图标。如果是文件夹,则显示文件夹图标。如果是文件,则显示文件图标。
I'm trying to do something pretty simple. I want to place an icon in a column for a particular row in a table. If it's a folder, display a folder icon. If it's a file, display a file icon.
有人知道如何在JavaFX 2中执行此操作吗?
Does anyone know how to do this in JavaFX 2?
我已经尝试了很多东西,这看起来应该很简单,或者至少是一个例子。
I've tried so many things and this seems like it should be pretty simple or at least an example somewhere.
推荐答案
好的,所以我有一个巨大的虚拟时刻。原来我的图片网址路径错了。
Okay so I had a huge dummy moment. Turns out that I had my image url path wrong.
我找到了一个网站,它为添加表格元素提供了一个很好的例子。这有助于我理解一切。
I did find a site that provides a great example for adding elements for table. This helped me understand everything.
现在,如果我之前尝试过的4种不同的方法都可行,我不知道,因为我的图片网址路径错误。但无论如何这里是链接和代码片段。
Now if the 4 different ways I tried before would've worked, I don't know because my image url path was wrong. But anyway here is the link and a code snippet.
底线是您需要 CellValueFactory
和 CellFactory
。我试图使用其中一个或。 TableCell中的 updateItem
模板方法依赖于从 CellValueFactory
中提取的值。
Bottom line was that you need to have the CellValueFactory
and the CellFactory
. I was attempting to use either or. The updateItem
template method in TableCell relies on the value dervied from 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;
}
});
这篇关于将图像放入JavaFX 2表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!