为什么我的JavaFX TableView为空? [英] Why is my JavaFX TableView empty?

查看:94
本文介绍了为什么我的JavaFX TableView为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充一个 TableView ,我不知道为什么它是空的。我有以下代码:

I want to populate a TableView and I don't know why it's empty. I have the following code:

public class StudentTeacherManagementController {
    private Client client = ClientBuilder.newClient();
    private Gson g = new Gson();
    private ArrayList<Schueler> schueler = new ArrayList<Schueler>();
    private ArrayList<Lehrer> lehrer = new ArrayList<Lehrer>();
    private final String REST_SERVICE_URL = "http://localhost:8080/A07_Webservice/rest/ManagementService";

    @FXML
    private TableView<Schueler> tblSchueler;
    @FXML
    private TableColumn<Schueler, String> lastnameCol;
    @FXML
    private TableColumn<Schueler, String> idCol;
    @FXML
    private TableColumn<Schueler, String> firstnameCol;
    @FXML
    private TableColumn<Schueler, String> birthdateCol;
    @FXML
    private TableColumn<Schueler, String> svnrCol;

    @FXML
    private TableView<Lehrer> tblLehrer;
    @FXML
    private TableColumn<Lehrer, String> svnrLehrerCol;
    @FXML
    private TableColumn<Lehrer, String> firstnameLehrerCol;
    @FXML
    private TableColumn<Lehrer, String> lastnameLehrerCol;
    @FXML
    private Button btnGetSchueler;
    @FXML
    private Button btnGetLehrer;

    @FXML
    public void btnGetSchueler() {
        tblSchueler.getItems().removeAll(tblSchueler.getItems());

        setCellConfigurationsSchueler();

        Type t = new TypeToken<List<Schueler>>() {}.getType();
        String s = client.target(REST_SERVICE_URL).path("schueler").request(MediaType.APPLICATION_JSON).get(String.class);
        List<Schueler> sch = g.fromJson(s, t);
        schueler = (ArrayList<Schueler>) sch;
        ObservableList<Schueler> oListSchueler = FXCollections.observableArrayList(schueler);

        tblSchueler.getItems().addAll(oListSchueler);
    }

    @FXML
    public void btnGetLehrer() {
        tblLehrer.getItems().removeAll(tblLehrer.getItems());

        setCellConfigurationsLehrer();

        Type t = new TypeToken<List<Lehrer>>() {}.getType();
        String s = client.target(REST_SERVICE_URL).path("lehrer").request(MediaType.APPLICATION_JSON).get(String.class);
        List<Lehrer> l = g.fromJson(s, t);
        lehrer = (ArrayList<Lehrer>) l;
        ObservableList<Lehrer> oListLehrer = FXCollections.observableArrayList(lehrer);

        tblLehrer.getItems().addAll(oListLehrer);
    }

这是我的 .fxml - 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="311.0" prefWidth="854.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.StudentTeacherManagementController">
    <children>
        <TableView fx:id="tblSchueler" editable="true" layoutX="420.0" layoutY="14.0" prefHeight="249.0" prefWidth="423.0">
            <columns>
                <TableColumn fx:id="idCol" prefWidth="87.0" text="ID" />
                <TableColumn fx:id="firstnameCol" prefWidth="83.0" text="Firstname" />
                <TableColumn fx:id="lastnameCol" prefWidth="86.0" text="Lastname" />
                <TableColumn fx:id="birthdateCol" prefWidth="51.0" text="Birthdate" />
                <TableColumn fx:id="svnrCol" prefWidth="93.0" text="SVNR" />
            </columns>
        </TableView>

        <TableView fx:id="tblLehrer" editable="true" layoutX="14.0" layoutY="14.0" prefHeight="249.0" prefWidth="328.0">
            <columns>
                <TableColumn fx:id="svnrLehrerCol" prefWidth="87.0" text="SVNR" />
                <TableColumn fx:id="firstnameLehrerCol" prefWidth="83.0" text="Firstname" />
                <TableColumn fx:id="lastnameLehrerCol" prefWidth="88.0" text="Lastname" />
            </columns>
        </TableView>

        <Button fx:id="btnGetSchueler" layoutX="632.0" layoutY="269.0" mnemonicParsing="false" onAction="#btnGetSchueler" text="Get Schueler" />
        <Button fx:id="btnGetLehrer" layoutX="132.0" layoutY="269.0" mnemonicParsing="false" onAction="#btnGetLehrer" text="Get Lehrer" />
    </children>
</AnchorPane>

你知道为什么这个表是空的吗?此外,这是 Schueler 类。我没有得到任何错误,为什么这段代码不起作用?

Do you know why this table is empty? Moreover, here is the Schueler class. I get no error so why doesn't this code work?

public class Lehrer {
    private String svnr;
    private String vorname; 
    private String nachname;

    public Lehrer(String svnr, String vorname, String nachname) {
        this.svnr = svnr;
        this.vorname = vorname;
        this.nachname = nachname;
    }

    public void setSvnr(String svnr) { this.svnr = svnr; }

    public String getSvnr() { return svnr; }

    public void setVorname(String vorname) { this.vorname = vorname; }

    public String getVorname() { return vorname; }

    public void setNachname(String nachname) { this.nachname = nachname; }

    public String getNachname() { return nachname; }
}

任何帮助将不胜感激!我可以从webservice加载数据,这就是为什么列表不为空但我仍然无法填充tableview。

Any help would be appreciated! I can load the data from the webservice and that's why the list isn't empty but I still can't populate the tableview.

推荐答案

首先,您必须在initialize方法中为表的每一列设置Cell Value Factory:

First of all, you must set Cell Value Factory for each column of the table in initialize method:

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        lastnameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        ...

而不是 tableView.getItems.addAll 使用 tableView.setItems

这篇关于为什么我的JavaFX TableView为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆