【问题标题】:Can't fill Table with items with javafx无法用 javafx 的项目填充表
【发布时间】:2013-12-01 14:52:55
【问题描述】:

我不太擅长 javafx 和英语^^。我编写了一个 TableView,但我不知道为什么它没有用我创建的项目填充表格。也许它填满了表格,但我没有看到它们。我希望有人可以在这里帮助我。这是代码:

        private final TableView<VerzeichnisDaten> table = new TableView();

        private final ObservableList<VerzeichnisDaten> data = FXCollections.observableArrayList();

        TableColumn titleCol = new TableColumn("Titel");
        TableColumn lastNameCol = new TableColumn("Nachname");
        TableColumn firstNameCol = new TableColumn("Vorname");
        TableColumn TelCol = new TableColumn("Telefon");
        TableColumn FaxCol = new TableColumn("Fax");
        TableColumn EmailCol = new TableColumn("E-Mail");

// here is a loop which wait for a mouse click on an item            
// item will be saved in abteilung

        try {
        VD.mitarbeiter(abteilung);
        } catch (SQLException ex) {
        /* exception */);
        }

        centerPane.getChildren().clear();
        table.getColumns().clear();
        data.clear();

        for(int j = 0;j < VD.count_mi;j++) {
        data.add(new VerzeichnisDaten(VD.title_speicher[j], VD.lname_speicher[j], VD.fname_speicher[j], VD.tel_speicher[j], VD.fax_speicher[j],VD.email_speicher[j] ));
        }

        titleCol.setMinWidth(100);
        titleCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("Titel"));
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("Nachname"));
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("Vorname"));
        TelCol.setMinWidth(100);
        TelCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("Telefon"));
        FaxCol.setMinWidth(100);
        FaxCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("Fax"));
        EmailCol.setMinWidth(100);
        EmailCol.setCellValueFactory (new PropertyValueFactory<VerzeichnisDaten, String>("E-Mail"));

        table.setItems(data);
        table.getColumns().addAll(titleCol,lastNameCol,firstNameCol,TelCol,FaxCol,EmailCol);
        centerPane.getChildren().addAll(table);

    mainPane.setCenter(centerPane);                                                              
    }
    primaryStage.setScene(new Scene(mainPane, 855, 400));
    primaryStage.show();

这是 VerzeichnisDaten 类:

String[] title_speicher, lname_speicher, fname_speicher, tel_speicher, fax_speicher, email_speicher;


SimpleStringProperty title, lastName, firstName, Tel, Fax, Email;

public VerzeichnisDaten (String title, String lname, String fname, String tel, String fax, String email) {

this.title = new SimpleStringProperty(title);
this.lastName = new SimpleStringProperty(lname);
this.firstName = new SimpleStringProperty(fname);
this.Tel = new SimpleStringProperty(tel);
this.Fax = new SimpleStringProperty(fax);
this.Email = new SimpleStringProperty(email);       
}

// Setter and Getter are now implemented, only not shown

此代码属于 VerzeichnisDaten。上面是更多的代码,但是 现在不相关。

void mitarbeiter (String Abteilung) throws SQLException {

// more code ...

stmt = conn.createStatement();
rset = stmt.executeQuery(sql_mi_stmt);
i = 0;
while (rset.next()){
      title_speicher[i] = rset.getString("title");
lname_speicher[i] = rset.getString("lname");
      fname_speicher[i] = rset.getString("fname");
      tel_speicher[i] = rset.getString("tel");
      fax_speicher[i] = rset.getString("fax");
      email_speicher[i] = rset.getString("email");

    i = i + 1; 
}
stmt.close();    
}

【问题讨论】:

  • 请尽量减少你的例子。
  • sry,我是新来的,但我会在必要的部分进行编辑
  • stackoverflow.com/questions/18497699/… 试试这个......你没有为java文件制作setter getter方法
  • 哦,是的,你是对的 :-) 。 Setter 和 Getter 没有实现,以为它们不重要。但是现在还有另一个问题:-(。表格只显示“电话”和“传真”列中的数字。为什么它忽略字符串?

标签: javafx tableview javafx-2 fxml fill


【解决方案1】:

您提供给PropertyValueFactory 的字符串,例如就像这里:

new PropertyValueFactory<VerzeichnisDaten, String>("Titel")

必须与数据类中属性的变量名匹配。

也就是说,如果你有:

class Person {
     StringProperty firstNameProperty;  // note: must end in "Property", per convention
     StringProperty lastNameProperty;
}

相应的属性值工厂将是:

new PropertyValueFactory<Person, String>("firstName")  // property name sans "Property" postfix
new PropertyValueFactory<Person, String>("lastName")

在您的代码中,您 1) 具有不以“Property”结尾的属性名称,即您有:

SimpleStringProperty title, lastName, firstName, Tel, Fax, Email;

和 2) 您在属性值工厂中使用了列标题的名称而不是属性名称。

因此,您发现了基于字符串或一般类型不安全的编程的乐趣:编译器很高兴,但没有任何工作。

随 JavaFX 提供的 PropertyValueFactory 充其量只是一种 hack,更普遍地说是一种不好的做法。如果您查看它的 Javadoc,您会确切地看到它的作用:它提供了一种更简单的方法来创建值工厂,而不必通过使用反射和特殊命名约定(后者您可以使用是受害者)找到正确的财产价值。

使用 Java 8 的 lambda 语法,您可以以更简单的方式编写这些值工厂,但我不建议使用 PropertyValueFactory

【讨论】:

  • 您可以通过单击投票点下方的复选标记将答案标记为正确。
猜你喜欢
  • 2012-11-23
  • 1970-01-01
  • 2015-11-09
  • 2017-02-02
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
相关资源
最近更新 更多