我正在尝试将数据显示为JavaFX TableView
形式的MySQL
数据库,其中的一列将包含来自
数据库。但除了图片列外,所有列均已填充。我的代码如下:
SrcLnugsCandidate
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class SrcLnugsCandidate {
private final SimpleStringProperty candID;
private final SimpleStringProperty candFirstname;
private final SimpleStringProperty candLastname;
private final SimpleStringProperty candPosition;
private ImageView image;
public SrcLnugsCandidate(String candID, String candFirstname, String candLastname, String candPosition,ImageView imageView) throws IOException {
this.candID = new SimpleStringProperty(candID);
this.candFirstname = new SimpleStringProperty(candFirstname);
this.candLastname = new SimpleStringProperty(candLastname);
this.candPosition = new SimpleStringProperty(candPosition);
this.image = imageView;
}
public ImageView getIv() {
return image;
}
public final void setIv(ImageView imageView) {
this.image = imageView;
}
public final SimpleStringProperty candIDProperty() {
return this.candID;
}
public final String getCandID() {
return this.candIDProperty().get();
}
public final void setCandID(final String candID) {
this.candIDProperty().set(candID);
}
public final SimpleStringProperty candFirstnameProperty() {
return this.candFirstname;
}
public final String getCandFirstname() {
return this.candFirstnameProperty().get();
}
public final void setCandFirstname(final String candFirstname) {
this.candFirstnameProperty().set(candFirstname);
}
public final SimpleStringProperty candLastnameProperty() {
return this.candLastname;
}
public final String getCandLastname() {
return this.candLastnameProperty().get();
}
public final void setCandLastname(final String candLastname) {
this.candLastnameProperty().set(candLastname);
}
public final SimpleStringProperty candPositionProperty() {
return this.candPosition;
}
public final String getCandPosition() {
return this.candPositionProperty().get();
}
public final void setCandPosition(final String candPosition) {
this.candPositionProperty().set(candPosition);
}
}
BallotScreenController
package application;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
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 BallotScreenController implements Initializable{
@FXML
TableView<SrcLnugsCandidate> candTV;
@FXML
TableColumn<SrcLnugsCandidate, String> candIDTC;
@FXML
TableColumn<SrcLnugsCandidate, String> fnameTC;
@FXML
TableColumn<SrcLnugsCandidate, String> lnameTC;
@FXML
TableColumn<SrcLnugsCandidate, String> policiesTC;
@FXML
TableColumn<SrcLnugsCandidate, ImageView> photoTC;
@FXML
ImageView iv1;
Connection myConnection = DBConnection.dbConnect();
ResultSet rsS;
private ObservableList<SrcLnugsCandidate> data2;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
this.data2 = FXCollections.observableArrayList();
rsS = myConnection.createStatement().executeQuery("SELECT * FROM srcpresident");
while (rsS.next()) {
InputStream is = rsS.getBinaryStream(6);
OutputStream os = new FileOutputStream(new File("photo.jpg"));
byte[] contents = new byte[1024];
int size = 0;
while((size = is.read(contents)) != -1) {
os.write(contents, 0 , size);
}
Image image = new Image("file:photo.jpg");
iv1.setImage(image);
this.data2.add(new SrcLnugsCandidate(rsS.getString(1), rsS.getString(2), rsS.getString(3), rsS.getString(5),iv1));
this.candIDTC.setCellValueFactory(new PropertyValueFactory<SrcLnugsCandidate,String>("candID"));
this.fnameTC.setCellValueFactory(new PropertyValueFactory<SrcLnugsCandidate,String>("candFirstname"));
this.lnameTC.setCellValueFactory(new PropertyValueFactory<SrcLnugsCandidate,String>("candLastname"));
this.policiesTC.setCellValueFactory(new PropertyValueFactory<SrcLnugsCandidate,String>("candPosition"));
this.photoTC.setCellValueFactory(new PropertyValueFactory<SrcLnugsCandidate,ImageView>("imageView"));
this.candTV.setItems(null);
this.candTV.setItems(data2);
}
} catch (SQLException | IOException e) {
System.out.println(e);
}
}
}