java 将图像放入 JavaFX 2 表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16341583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing an image into a JavaFX 2 table
提问by Jamel Toms
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.
我正在尝试做一些非常简单的事情。我想在表中特定行的列中放置一个图标。如果是文件夹,则显示文件夹图标。如果是文件,则显示文件图标。
Does anyone know how to do this in JavaFX 2?
有谁知道如何在 JavaFX 2 中做到这一点?
I've tried so many things and this seems like it should be pretty simple or at least an example somewhere.
我已经尝试了很多东西,这看起来应该很简单,或者至少是某个地方的一个例子。
回答by Jamel Toms
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.
我确实找到了一个网站,它提供了一个很好的例子来为表格添加元素。这帮助我理解了一切。
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.
现在,如果我之前尝试过的 4 种不同方式都有效,我不知道,因为我的图像 url 路径是错误的。但无论如何这里是链接和代码片段。
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
.
最重要的是,您需要拥有CellValueFactory
和CellFactory
。我试图使用或。updateItem
TableCell 中的模板方法依赖于从 派生的值CellValueFactory
。
http://blog.ngopal.com.np/2011/10/01/tableview-cell-modifiy-in-javafx/
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;
}
});
回答by Cyrus13
In case the provided answers did not work for you (like it didn't for me), this was the solution I found (Of course you still needs to create the tableView and add the columns to it):
如果提供的答案对您不起作用(就像它对我不起作用),这是我找到的解决方案(当然,您仍然需要创建 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())));
}