JavaFX表格视图显示Image@Hashcode而不是显示模型中的图像本身
我试图在表视图中显示汽车对象的图像,我创建了CardSplayModel类,以便在表列中显示可观察集合中的相关值,我有2列包含字符串值和一个整数,所有这些都显示良好,我也有一个汽车的图像,我想显示,但它只会显示Image@Hashcode而不是图像本身 我目前的实施情况如下: 显示模型类:JavaFX表格视图显示Image@Hashcode而不是显示模型中的图像本身,image,javafx,tableview,observablecollection,Image,Javafx,Tableview,Observablecollection,我试图在表视图中显示汽车对象的图像,我创建了CardSplayModel类,以便在表列中显示可观察集合中的相关值,我有2列包含字符串值和一个整数,所有这些都显示良好,我也有一个汽车的图像,我想显示,但它只会显示Image@Hashcode而不是图像本身 我目前的实施情况如下: 显示模型类: private ObjectProperty<Image> CarImage; private SimpleStringProperty CarMake; private SimpleStri
private ObjectProperty<Image> CarImage;
private SimpleStringProperty CarMake;
private SimpleStringProperty CarModel;
private SimpleDoubleProperty CarPrice;
public CarDisplayModel(Image carImage, String carMake, String carModel, Double carPrice) {
this.CarImage = new SimpleObjectProperty<>(carImage);
this.CarMake = new SimpleStringProperty(carMake);
this.CarModel = new SimpleStringProperty(carModel);
this.CarPrice = new SimpleDoubleProperty(carPrice);
}
public Image getCarImage() {
return CarImage.get();
}
public void setCarImage(Image displayCarImage) {
this.CarImage = new SimpleObjectProperty<>(displayCarImage);
}
private ObjectProperty CarImage;
卡马克私人住宅;
私人SimpleStringProperty CarModel;
私人简单双重产权;
公共CardSplayModel(图像carImage、字符串carMake、字符串carModel、双carPrice){
this.CarImage=新的SimpleObject属性(CarImage);
this.CarMake=新的SimpleStringProperty(CarMake);
this.CarModel=新的SimpleStringProperty(CarModel);
this.CarPrice=新的简化双重属性(CarPrice);
}
公共映像getCarImage(){
返回CarImage.get();
}
公共无效设置CARIMAGE(图像显示CARIMAGE){
this.CarImage=新的SimpleObject属性(displayCarImage);
}
控制器上FXML文件初始化方法的实现:
try {
OutputStream targetFile = new FileOutputStream(s + "\\" + imageUrl);
targetFile.write(fileBytes);
targetFile.close();
File f = new File(s + imageUrl);
carImage = new Image(f.toURI().toString());
} catch (IOException e) {
e.printStackTrace();
}
Car car = new Car(regplate, mileage, bhp, price, engineSize, topSpeed, make, model, carImage, imageUrl);
CarMake.setCellValueFactory(new PropertyValueFactory<>("CarMake"));
CarModel.setCellValueFactory(new PropertyValueFactory<>("CarModel"));
CarPrice.setCellValueFactory(new PropertyValueFactory<>("CarPrice"));
CarImage.setCellValueFactory(new PropertyValueFactory<>("CarImage"));
ObservableList<CarDisplayModel> carDisplayModel = FXCollections.observableArrayList(
new CarDisplayModel(car.getCarDisplayPicture(), car.getMake(), car.getModel(), car.getPrice()));
carSelectionTable.setItems(carDisplayModels);
}
试试看{
OutputStream targetFile=新文件OutputStream(s+“\\”+imageUrl);
targetFile.write(fileBytes);
targetFile.close();
文件f=新文件(s+imageUrl);
carImage=新图像(f.toURI().toString());
}捕获(IOE异常){
e、 printStackTrace();
}
汽车=新车(车牌、里程、必和必拓、价格、发动机规格、最高速度、品牌、型号、carImage、imageUrl);
CarMake.setCellValueFactory(新财产价值工厂(“CarMake”);
CarModel.setCellValueFactory(新属性价值工厂(“CarModel”);
CarPrice.setCellValueFactory(新财产价值工厂(“CarPrice”);
CarImage.setCellValueFactory(新属性值工厂(“CarImage”);
ObservableList CardSplayModel=FXCollections.observableArrayList(
新的CardSplayModel(car.getCardSplayPicture()、car.getMake()、car.getModel()、car.getPrice());
carSelectionTable.setItems(CardSplayModels);
}
我当前获得的输出:
使用ImageView控制器并尝试显示相同的图像可以很好地显示它,因此我想知道这是与ObjectProperty实现还是与TableView控件有关。。我最近遇到了一个类似且更常见的问题,即需要重写toString方法以返回实际值,而不是String@HashValue,我想知道是否有类似的方法来解决这个问题
非常感谢您的时间提供任何建议。这与默认的
cellFactory
返回的TableCell
的updateItem
实现有关。其实现如下(忽略空单元格):
- 如果项目是
,则使用节点
将项目作为参数显示。这会将setGraphic
添加到节点
内的场景中表格单元格
- 否则,使用对象的
方法将对象转换为文本,并使用toString
将结果值显示为setText
表格单元格中的文本
图像
不是节点的子类
;因此,您会得到后一种行为
要更改此行为,您需要使用自定义的cellFactory
,以正确处理项目类型:
CarImage.setCellFactory(col -> new TableCell<Car, Image>() { // assuming model class named Car here; type parameters match the TableColumn
private final ImageView imageView = new ImageView();
{
// set size of ImageView
imageView.setFitHeight(50);
imageView.setFitWidth(80);
imageView.setPreserveRatio(true);
// display ImageView in cell
setGraphic(imageView);
}
@Override
protected void updateItem(Image item, boolean empty) {
super.updateItem(item, empty);
imageView.setImage(item);
}
});
CarImage.setCellFactory(col->new TableCell(){//假设此处的模型类名为Car;类型参数与TableColumn匹配
private final ImageView ImageView=新建ImageView();
{
//设置ImageView的大小
imageView.setFitHeight(50);
imageView.setFitWidth(80);
imageView.setPreserveRatio(真);
//在单元格中显示ImageView
设置图形(图像视图);
}
@凌驾
受保护的void updateItem(图像项,布尔值为空){
super.updateItem(项,空);
imageView.setImage(项目);
}
});
请注意,通常不会为空单元格保留图形。我决定在这种情况下这样做是为了保持单元格大小的一致性。与您的问题无关:请学习java命名约定并遵守它们。感谢您的输入,我尝试使用CamelCase约定,但不太确定我的错误在哪里,您能给我指出一些吗?或者提供相关资源让我看一下吗?字段名必须以小写字母开头:)