我在JavaFX中的表视图有问题。我知道论坛中还有其他一些帖子,但我检查了它们并尝试使用这些解决方案,但对我来说没有任何用处......我有3节课。一个版本类,即数据模型。将数据填充到表视图的类,以及加载表并在 UI 中显示的类。你能看出我做错了什么吗?
版本类(表的数据模型)
public class Version {
private final SimpleStringProperty aktVer;
private final SimpleStringProperty produktDate;
private final SimpleStringProperty aktVerForCustom;
private final SimpleStringProperty lastVer;
private final SimpleStringProperty lastVerForCustom;
private final SimpleStringProperty runOut;
Version(String aktVer, String produktDate, String aktVerForCustom, String lastVer, String lastVerForCustom, String runOut) {
this.aktVer = new SimpleStringProperty(aktVer);
this.produktDate = new SimpleStringProperty(produktDate);
this.aktVerForCustom = new SimpleStringProperty(aktVerForCustom);
this.lastVer = new SimpleStringProperty(lastVer);
this.lastVerForCustom = new SimpleStringProperty(lastVerForCustom);
this.runOut = new SimpleStringProperty(runOut);
}
public String getAktVer() {
return aktVer.get();
}
public void setAktVer(String aktVer) {
this.aktVer.set(aktVer);
}
public String getProduktDate() {
return produktDate.get();
}
public void setProduktDate(String produktDate) {
this.produktDate.set(produktDate);
}
public String getAktVerForCustom() {
return aktVerForCustom.get();
}
public void setAktVerForCustom(String aktVerForCustom) {
this.aktVerForCustom.set(aktVerForCustom);
}
public String getLastVer() {
return lastVer.get();
}
public void setLastVer(String lastVer) {
this.lastVer.set(lastVer);
}
public String getLastVerForCustom() {
return lastVerForCustom.get();
}
public void setLastVerForCustom(String lastVerForCustom) {
this.lastVerForCustom.set(lastVerForCustom);
}
public String getRunOut() {
return runOut.get();
}
public void setRunOut(String runOut) {
this.runOut.set(runOut);
}
public void print(){
System.out.println("Daten:"+aktVer+" "+produktDate+" "+aktVerForCustom+" "+lastVer+" "+lastVerForCustom+" "+runOut);
}
}
表类:(用数据填充表)
public class VersionsManagerTabelController implements Initializable {
Connection con = null;
Statement st = null;
ResultSet rs = null;
ControllerManager cm = new ControllerManager();
DatabaseConnector dc = DatabaseConnector.getInstance();
ObservableList<Version> versionen = FXCollections.observableArrayList();
@FXML
TableColumn tc_aktVer;
@FXML
TableColumn tc_produktDate;
@FXML
TableColumn tc_aktVerForCustom;
@FXML
TableColumn tc_lastVer;
@FXML
TableColumn tc_lastVerForCustom;
@FXML
TableColumn tc_runOut;
@FXML
TableView tv_versions;
public void populateDataForTableView(String product) throws SQLException{
if(product.matches("toolstarLX")){
versionen.removeAll(versionen);
con = dc.getConnectionToDB();
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM testlx");
if (rs.next()) {
String ver = rs.getString("akt_ver");
String pd = rs.getString("produkt_datum");
String ak = rs.getString("an_kunden");
String laver = rs.getString("letzte_ver");
String lvforcustom = rs.getString("lv_an_kunden");
String runout = rs.getString("lauft_ab_am");
versionen.add(new Version(ver, pd, ak, laver, lvforcustom, runout));
versionen.get(0).print();
}
tv_versions.setItems(versionen);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {;
tc_aktVer.setCellValueFactory(new PropertyValueFactory<Version, String>("aktVer"));
}
}
表类:(仅加载内容并显示表的方法)
@FXML
private void showLxTabel(ActionEvent event) {
try {
cm.setProduct(lx);
tableCont.populateDataForTableView(cm.getProduct());
AnchorPane root=null;
try {
root = FXMLLoader.load(getClass().getResource("VersionsManagerTabel.fxml"));
} catch (IOException ex) {}
ap_tabelContent.getChildren().clear();
ap_tabelContent.getChildren().add(root);
lbl_prduct_overview.setText(lx);
} catch (SQLException ex) {Logger.getLogger(VersionsManagerMainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="440.0" prefWidth="598.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="toolhouseversionsmanager.VersionsManagerTabelController">
<children>
<TableView fx:id="tv_versions" layoutX="14.0" layoutY="14.0" prefHeight="413.0" prefWidth="570.0">
<columns>
<TableColumn fx:id="tc_aktVer" prefWidth="76.0" text="akt. Version" />
<TableColumn fx:id="tc_produktDate" prefWidth="117.0" text="Produktionsdatum" />
<TableColumn fx:id="tc_aktVerForCustom" prefWidth="113.0" text="akt Ver. an Kunden" />
<TableColumn fx:id="tc_lastVer" minWidth="0.0" prefWidth="96.0" text="letzte Version" />
<TableColumn fx:id="tc_lastVerForCustom" prefWidth="127.0" text="letz. Ver. an Kunden" />
<TableColumn fx:id="tc_runOut" prefWidth="127.0" text="läuft ab am" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
这是我获取控制器的代码。我经常遇到控制器及其引用的问题,但使用这种方法它效果很好(直到现在??在其他类中,我创建了一个新变量,例如VersionsManagerTableController example = ControllerManager.getTableController()
public class ControllerManager {
//Enthält den String des momentan ausgeählten Produkts (für Erstellen, löschen und bearbeiten von Einträgen
//in der Datenbank
StringProperty product = new SimpleStringProperty();
public String getProduct(){
return product.get();
}
public void setProduct(String product){
this.product.set(product);
}
public StringProperty productProperty(){
return product;
}
//Gibt eine Referenz auf den Controller von VersionsManagerNew.fxml (Neuen Eintrag anlegen)
public VersionsManagerNewController getNewEntryController() throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader();
Pane p = fxmlLoader.load(getClass().getResource("VersionsManagerNew.fxml").openStream());
VersionsManagerNewController controller = (VersionsManagerNewController) fxmlLoader.getController();
return controller;
}
public VersionsManagerTabelController getTableController() throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader();
Pane p = fxmlLoader.load(getClass().getResource("VersionsManagerTabel.fxml").openStream());
VersionsManagerTabelController controller = (VersionsManagerTabelController) fxmlLoader.getController();
return controller;
}
}
你对VersionsManagerTabelController
的引用,tableCont
,不是指控制你实际显示的 UI 的同一个控制器实例。您的showLxTabel
方法应如下所示(我不知道您在此处的所有方法调用都做了什么,因为您没有发布完整的示例,但这应该可以解决眼前的问题):
@FXML
private void showLxTabel(ActionEvent event) {
try {
cm.setProduct(lx);
// tableCont.populateDataForTableView(cm.getProduct());
AnchorPane root=null;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("VersionsManagerTabel.fxml"));
root = loader.load();
VersionsManagerTabelController tableController = loader.getController();
tableController.populateDataForTableView(cm.getProduct());
} catch (IOException ex) {
ex.printStackTrace();
}
ap_tabelContent.getChildren().clear();
ap_tabelContent.getChildren().add(root);
lbl_prduct_overview.setText(lx);
} catch (SQLException ex) {Logger.getLogger(VersionsManagerMainController.class.getName()).log(Level.SEVERE, null, ex);
}