有人可以告诉我,为什么一切正常,除了在表格中显示ID?数据类型有问题吗?
有一个代码,我将数据放入tableView。
public class TabulkaProdukty {
private final FlowPane panelTabulky;
public final TableView<Produkty> tabulkaTabulkaProdukty;
public ObservableList<Produkty> dataTabulkaProdukty;
public static Connection connection;
private static Statement statement;
public TabulkaProdukty() {
panelTabulky = new FlowPane();
tabulkaTabulkaProdukty = new TableView<>();
panelTabulky.getChildren().add(tabulkaTabulkaProdukty);
tabulkaTabulkaProdukty.setEditable(true);
try {
connection = DriverManager.getConnection("jdbc:sqlite:produkty");
statement = connection.createStatement();
statement.executeUpdate("create table if not exists produkty(id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, nazev TEXT NOT NULL, popis TEXT NOT NULL, typ TEXT NOT NULL, cena TEXT NOT NULL);");
dataTabulkaProdukty = FXCollections.observableArrayList();
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM produkty;");
while (rs.next()) {
dataTabulkaProdukty.add(new Produkty(rs.getInt("id"), rs.getString("nazev"), rs.getString("popis"), rs.getString("typ"), rs.getString("cena")));
}
// VECI V TABULCE
TableColumn idCol = new TableColumn("id");
idCol.setMinWidth(50);
idCol.setCellValueFactory(
new PropertyValueFactory<>("id"));
TableColumn nazevCol = new TableColumn("Název");
nazevCol.setMinWidth(100);
nazevCol.setCellValueFactory(
new PropertyValueFactory<>("nazev"));
TableColumn popisCol = new TableColumn("Popis");
popisCol.setMinWidth(200);
popisCol.setCellValueFactory(
new PropertyValueFactory<>("popis"));
TableColumn typCol = new TableColumn("Typ");
typCol.setMinWidth(50);
typCol.setCellValueFactory(
new PropertyValueFactory<>("typ"));
TableColumn cenaCol = new TableColumn("Cena");
cenaCol.setMinWidth(50);
cenaCol.setCellValueFactory(
new PropertyValueFactory<>("cena"));
tabulkaTabulkaProdukty.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tabulkaTabulkaProdukty.setItems(null);
tabulkaTabulkaProdukty.setItems(dataTabulkaProdukty);
tabulkaTabulkaProdukty.getColumns().addAll(idCol, nazevCol, popisCol, typCol, cenaCol);
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error dialog");
alert.setHeaderText("Chyba databáze");
alert.setContentText("Nepovedlo se načíst data z databáze.");
alert.showAndWait();
}
}
有一个Class Produkty
public class Produkty implements Serializable {
private final SimpleIntegerProperty id;
private final SimpleStringProperty nazev;
private final SimpleStringProperty popis;
private final SimpleStringProperty typ;
private final SimpleStringProperty cena;
public NaMinimum panelNaMinimum;
public Produkty(int pID, String pNazev, String pPopis, String pTyp, String pCena) {
this.id = new SimpleIntegerProperty(pID);
this.nazev = new SimpleStringProperty(pNazev);
this.popis = new SimpleStringProperty(pPopis);
this.typ = new SimpleStringProperty(pTyp);
this.cena = new SimpleStringProperty(pCena);
}
public int getIDProduktu() {return id.get();}
public void setIDProduktu(int pID) {id.set(pID);}
public String getNazev() {return nazev.get();}
public void setNazev(String pNazev) {nazev.set(pNazev);}
public String getPopis() {return popis.get();}
public void setPopis(String pPopis) {popis.set(pPopis);}
public String getTyp() {return typ.get();}
public void setTyp(String pTyp) {typ.set(pTyp);}
public String getCena() {return cena.get();}
public void setCena(String pCena) {cena.set(pCena);}
}
我真的很感激任何解决的建议。
答案 0 :(得分:2)
PropertyValueFactory
根据构造函数参数查找方法,而不是字段。根据getter的名称,参数应为"IDProduktu"
或"iDProduktu"
:
idCol.setCellValueFactory(new PropertyValueFactory<>("IDProduktu"));
请注意,如果没有属性getter方法TableView
,则无法观察到属性中的任何更改。在这种情况下,您可以使用普通字段获得相同的效果。要观察更改,您需要提供一个名为<property>Property
的方法,其中<property>
是一个占位符,用于传递给PropertyValueFactory
的构造函数的属性名称,例如如果您使用"id"
作为构造函数参数:
public IntegerProperty idProperty() {
return id;
}
答案 1 :(得分:0)
最好知道你对此做了什么,但我的猜测是id的整数值没有显示为字符串,因此其行为方式与其他参数不同。 / p>