Javafx Tableview不显示ObservableList的数据

时间:2015-06-15 12:55:58

标签: user-interface javafx

我在JavaFX中遇到Tableview问题。我知道论坛上还有其他一些帖子,但是我检查过它们并尝试使用这些解决方案,但对我来说没什么用...我有3个类。一个Version类,数据模型。将数据填充到tableview的类,以及加载表并在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;
}

}

1 个答案:

答案 0 :(得分:0)

The reference you have to the VersionsManagerTabelController, tableCont, is not referring to the same controller instance that is controlling the UI you actually display. Your showLxTabel method should look something like this (I don't know what all the method calls you have here do, as you didn't post a complete example, but this should fix the immediate problem):

@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);
}