English 中文(简体)
无法从数据库中显示图像以图
原标题:Unable to display image from database to tableview
  • 时间:2024-01-14 11:31:36
  •  标签:
  • javafx

So I m trying to display images stored in my database as Blob to an image column in table view. All of the other data is displayed well except for those in the image column. I ve been struggling for days but still unable to find a solution.

这里我迄今为止所尝试的:

    public void loadPropertyData() throws IOException {
        try {
            Connection connection = OracleConnect.getConnection();
            String query = "SELECT id, property, image, price, contact, location, status FROM property" + getProfileName().getText();
            PreparedStatement statement = connection.prepareStatement(query);
            ResultSet resultSet = statement.executeQuery();
            ObservableList<Property> propertyList = FXCollections.observableArrayList();

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String property = resultSet.getString("property");
                Blob imageBlob = resultSet.getBlob("image");
                Image image = null;
                if (imageBlob != null) {
                    InputStream is = imageBlob.getBinaryStream();
                    image = new Image(is);
                }
                String price = resultSet.getString("price");
                String contact = resultSet.getString("contact");
                String location = resultSet.getString("location");
                String status = resultSet.getString("status");

                Property propertyObj = new Property(id, property, image, price, contact, location, status);
                propertyList.add(propertyObj);
            }

            propertyIDColumn.setCellValueFactory(cellData -> Bindings.createObjectBinding(() -> cellData.getValue().getId()));
            propertyColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getProperty()));
            propertyImageColumn.setCellFactory(param -> new TableCell<Property, ImageView>() {
                private final ImageView imageView = new ImageView();

                {
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }

                @Override
                protected void updateItem(ImageView item, boolean empty) {
                    super.updateItem(item, empty);

                    if (empty || item == null) {
                        imageView.setImage(null);
                        setText(null);
                        setGraphic(null);
                    } else {
                        imageView.setImage(item.getImage());
                        setGraphic(imageView);
                    }
                }
            });
            propertyPriceColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPrice()));
            propertyContactColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getContact()));
            propertyLocationColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getLocation()));
            propertyStatusColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStatus()));

            propertyTableView.setItems(propertyList);

            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

I would appreciate any help.

问题回答

首先是作为<代码>的可读密码>数据模型的类别。 在以下代码中,该类别为<代码>。 ImagItem。 该类别必须包含对<代码>的每个栏目的特性。 ObservableValue。 在以下代码中,只有一栏。 因此,<代码>ImagItem包含单一特性。 每项任务都需要三种方法。

  • One that returns the value wrapped by the ObservableValue.
  • One that assigns the wrapped value.
  • One that returns the ObservableValue.

现在,你需要界定 贵重值工厂<>和 贵重工厂<>,用于<编码>栏目。 贵重值工厂<>基本上是一种接收<>CellDataFeatures和回归的方法。 ObservableValue。 在以下代码中,作为这一方法的斜体表示中,c的一个实例。 CellDataFeatures<ImagItem, Value>及其getValue 方法回归一个<代码>ImagItem的事例,因此,请将ImagItem的属性归还<编码>。 注:类别<代码>ImagItem中的方法名称按照公认的公约确定。

最后,cellshop是一种接收<>Table Collumn并且需要退还tableCell的方法。 在以下代码中,我写了匿名内装课,暗中扩展类别:/code>,而且我有压倒一切的方法updateItem. 扩展Labeled,因此拥有text property and a ic <>>s>。 您请您的<代码>TableCell仅显示图像,因此,在以下代码中,我简单地将方法参数(img)填入“密码>,并将之作为<代码>的图表。 表Cell。

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class Test extends Application {
    private static final Path  ROOT = Paths.get("C:\Users\USER\Pictures");

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        TableView<ImagItem> table = new TableView<ImagItem>();
        TableColumn<ImagItem, Image> imgCol = new TableColumn<ImagItem, Image>("Image");
        imgCol.setCellValueFactory(c -> c.getValue().imageProperty());
        imgCol.setCellFactory(c -> {
            return new TableCell<ImagItem, Image>() {
                protected void updateItem(Image img, boolean empty) {
                    super.updateItem(img, empty);
                    setText(null);
                    if (img != null  &&  !empty) {
                        ImageView imgVw = new ImageView(img);
                        setGraphic(imgVw);
                    }
                    else {
                        setGraphic(null);
                    }
                }
            };
        });
        table.getColumns().add(imgCol);
        ObservableList<ImagItem> items = FXCollections.observableArrayList(getImages());
        table.setItems(items);
        root.getChildren().add(table);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private ImagItem getImage(String name) throws MalformedURLException {
        Path path = ROOT.resolve(name);
        URI uri = path.toUri();
        URL url = uri.toURL();
        Image img = new Image(url.toExternalForm());
        ImagItem ii = new ImagItem(img);
        return ii;
    }

    private List<ImagItem> getImages() throws MalformedURLException {
        List<ImagItem> list = new ArrayList<>();
        list.add(getImage("284kd.png"));
        list.add(getImage("dynamite.png"));
        return list;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class ImagItem {
    private SimpleObjectProperty<Image>  image;

    public ImagItem(Image img) {
        image = new SimpleObjectProperty<Image>(this, "image", img);
    }

    public Image getImage() {
        return image.get();
    }

    public void setImage(Image img) {
        image.set(img);
    }

    public SimpleObjectProperty<Image> imageProperty() {
        return image;
    }
}

下面是屏幕:

“crack”/





相关问题
JavaFX with Maven [closed]

I recently started a JavaFX project, and I d like to use Maven as my compiler/deployment tool. Is there a good tutorial or plugin to integrate JavaFX and Maven?

Displaying a string with variables in JavaFX

I have little quiz game I ve working on and am tracking correct answers. I have a text object that displays the number of consecutive correct answers. This is the content: content: bind consecutive....

non java-developer question

Just a basic question about Java (haven t really done anything with it personally yet): Is it possible to write a Java program that runs in a web browser (via JRE) on the client machine? Is ...

Pros and Cons of JavaFX and Silverlight [closed]

I know there is already a question about the performance of Flex, JavaFX, and Silverlight. My question is a bit more broad: We are evaluating the merits of JavaFX and Silverlight to serve as the GUI ...

How to simplify SVG code?

Is it possible to simplify / clean up svg code by replacing the use-tags with standard svg elements? Maybe an inkscape plugin? Haven t found anything... Background: I m converting some svgs to javafx ...

Dynamically changing a keyframes time attribute in JavaFX

I am making a game in JavaFX and have implemented a slider for controlling the game speed. I have a simple slider (javafx.scene.control.Slider) and I m binding the time attribute for the gameloop to ...

Migrate Java Applet to what/where?

I am reviewing currently a medium size code base (around 30K LOC) which uses a huge Applet and interfaces with other systems. It s a tool to create custom labels, so we need drag-n-drop and other ...

热门标签