为什么我的JavaFX TableView为空?

时间:2017-04-30 08:35:40

标签: java javafx tableview

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

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 - File

<?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类。我没有错误,为什么这段代码不起作用?

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。

1 个答案:

答案 0 :(得分:1)

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

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

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

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