无法用带有javafx的项填充表

无法用带有javafx的项填充表

本文介绍了无法用带有javafx的项填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not so good at javafx and also in English^^. I programmed a TableView but I don't know why it doesn't fill the Table with the Items I created. Perhaps it fills the Table, but I don't see them. I hope somebody could help me here out. Here is the Code:

        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类:

Here is the Class 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。在他们之上是更多的代码,但是
现在不相关。

this code belongs to VerzeichnisDaten. above them was more code but isnot relevant now.

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


推荐答案

您提供给的字符串 PropertyValueFactory ,例如如下:

The string that you supply to the PropertyValueFactory, e.g. as here:

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

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

must match the variable name of the property in your data class.

也就是说,如果你有:

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结尾的属性名称,即你有:

In your code, you 1) have property names that do not end with "Property", i.e. you have:

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

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

and 2) you have used the names of the columns headers instead of the property names in your property value factories.

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

And thus you have discovered the joys of string-based, or type-unsafe in general, programming: the compiler is happy, but nothing works.

JavaFX提供的 PropertyValueFactory 充其量只是一种黑客行为,而且通常是一种不好的做法。如果你看看它的Javadoc,你会看到它的作用:它提供了一种更简单的方法来创建一个值工厂,而不必处理丑陋的 Callback SAM,使用它反射和特殊命名约定(后者是您受害者)找到正确的属性值。

The PropertyValueFactory supplied with JavaFX is at best a hack, and more generally a bad practice. If you look at its Javadoc, you see exactly what it does: it provides a simpler way of creating a value factory instead of having to deal with the ugly Callback SAM, by using reflection and special naming conventions (the latter to which you were victim) to find the right property value.

使用Java 8的lambda语法,您可以用更简单的方式编写这些值工厂,并且我会忽略 PropertyValueFactory

With Java 8's lambda syntax you can write those value factories in a much simpler way and I would disadvice the usage of PropertyValueFactory.

这篇关于无法用带有javafx的项填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:40