Javafx 在 TableView 中添加图像

标签 java javafx javafx-2 javafx-8

我想在 tableView 中添加图片,但无法添加图片。

我在 byte[] 中获取图像,我可以在 imageView 中设置此图像,但是有什么方法可以将它添加到 tableView 中?

person3 类(class):

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

想得到任何建议。谢谢。

最佳答案

解决这个问题的一种可能方法是创建简单的类,比方说 CustomImage与私有(private)ImageView对象初始化及其setter和getter。接下来,您可以使用此类来指定 TableView<T>TableColumn<T>通用类型,设置列的单元格值工厂,并用您的图像填充表格。示例的实现 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<CustomImage> tableview = new TableView<CustomImage>();

        /* layout -> center -> tableview */

        /* initialize two CustomImage objects and add them to the observable list */
        ObservableList<CustomImage> 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<CustomImage, ImageView> firstColumn = new TableColumn<CustomImage, ImageView>("Images");
        firstColumn.setCellValueFactory(new PropertyValueFactory<CustomImage, ImageView>("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);
    }
}

这就是它的样子:

enter image description here

关于Javafx 在 TableView 中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933164/

相关文章:

java - JAX-WS异常

JavaFX 列表就像 SceneBuilder 中的库一样

JavaFX 按钮垃圾邮件 MouseExit 和 MouseEntered

JAVAFX :Create stage and text area at runtime

java - 如何在 Java 中定义配置参数?

java - 编码约定 - 命名枚举

java - Spring Cloud 配置服务器-登录错误

java - FilteredList 在更新时给出 java.lang.ArrayIndexOutOfBoundsException

JavaFX:从 TableView 获取列数据

JavaFX:tableView.getSelectionModel().select(0)不起作用的任何可能原因?