javafx在tableview中添加图像

我试图在tableview中添加图像,但我无法添加图像。

我在byte []中获取图像,我可以在imageview中设置此图像,但有没有办法在表视图中添加它。

人类:

public class person3 { private final StringProperty firstName7 = new SimpleStringProperty(""); private final StringProperty firstName8 = new SimpleStringProperty(""); public person3(String firstName4,String firstName5) { setFirstName7(firstName4); setFirstName8(firstName5); } public String getFirstName7() { return firstName7.get(); } public void setFirstName7(String name) { this.firstName7.set(name); } public StringProperty firstNameProperty7() { return firstName7; } public String getFirstName8() { return firstName8.get(); } public void setFirstName8(String name) { this.firstName8.set(name); } public StringProperty firstNameProperty8() { return firstName8; } } 

现在我想在tableview中添加图像

  f51=rs.getBytes(10); System.out.println(f51); ByteArrayInputStream bis = new ByteArrayInputStream(f51); System.out.println(bis); BufferedImage read = ImageIO.read(bis); System.out.println(read); Image image = SwingFXUtils.toFXImage(read, null); table1.getItems().add(new person3("Data1","data2")); // this add simple data but how can i add image 

帮我。

谢谢。

这个问题的一个可行的方法是创建简单的类,让我们说具有私有ImageView对象初始化的CustomImage及其setter和getter。 接下来,您可以使用此类指定TableViewTableColumngenerics类型,设置列的单元格值工厂,并使用图像填充表格。 示例CustomImage类的实现及其实际用途如下所示:

 import javafx.scene.image.ImageView; public class CustomImage { private ImageView image; CustomImage(ImageView img) { this.image = img; } public void setImage(ImageView value) { image = value; } public ImageView getImage() { return image; } } 

实际执行:

 import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class ImageViewInTableView extends Application { public Parent createContent() { /* layout */ BorderPane layout = new BorderPane(); /* layout -> center */ TableView tableview = new TableView(); /* layout -> center -> tableview */ /* initialize two CustomImage objects and add them to the observable list */ ObservableList imgList = FXCollections.observableArrayList(); CustomImage item_1 = new CustomImage(new ImageView(new Image("Icon_AddNewPatient.png"))); CustomImage item_2 = new CustomImage(new ImageView(new Image("Icon_EditPatient.png"))); imgList.addAll(item_1, item_2); /* initialize and specify table column */ TableColumn firstColumn = new TableColumn("Images"); firstColumn.setCellValueFactory(new PropertyValueFactory("image")); firstColumn.setPrefWidth(60); /* add column to the tableview and set its items */ tableview.getColumns().add(firstColumn); tableview.setItems(imgList); /* add TableView to the layout */ layout.setCenter(tableview); return layout; } @Override public void start(Stage stage) throws Exception { stage.setScene(new Scene(createContent())); stage.setWidth(200); stage.setHeight(200); stage.show(); } public static void main(String args[]) { launch(args); } } 

这就是它的样子:

在此处输入图像描述

我使用了你的代码但是使用了更新版本的Netbeans。 我还添加了一个String列。

 import javafx.scene.image.ImageView; public class CustomImage { private ImageView image; private String string; CustomImage(ImageView img, String string) { this.image = img; this.string = string; } public void setImage(ImageView value) { image = value; } public ImageView getImage() { return image; } public void setSring(String string) { this.string = string; } public String getString() { return this.string; } } 

在JavaFx Controller中实现。

 import java.net.URL; import java.util.ResourceBundle; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; public class FXMLDocumentController implements Initializable { //Going to need access to the table view. I used JavaFx Scene Builder to create the tableview. //You can use the .fxml document also. @FXML private TableView tvMain; @Override public void initialize(URL url, ResourceBundle rb) { /* initialize two CustomImage objects and add them to the observable list */ ObservableList imgList = FXCollections.observableArrayList(); CustomImage item_1 = new CustomImage(new ImageView(new Image("aulogo02.jpg")), "hello"); CustomImage item_2 = new CustomImage(new ImageView(new Image("aulogo02.jpg")), "world"); imgList.addAll(item_1, item_2); /* initialize and specify table column */ TableColumn tcC1 = new TableColumn<>("Picture"); tcC1.setCellValueFactory(new PropertyValueFactory<>("image")); tcC1.setPrefWidth(130);//set this to what you prefer. TableColumn tcX = new TableColumn<>("Text"); tcX.setCellValueFactory(new PropertyValueFactory<>("string")); /* add column to the tableview and set its items */ tvMain.getColumns().add(tcC1); tvMain.getColumns().add(tcX); tvMain.setItems(imgList); } } 

在FXML文件中实现。