Table 未更新新行

Table not updated with new row

我终于把内容写进了我的 table(因为我多年来一直在掌握这个谜团!)。现在我想将新项目添加到列表中并自动更新 table。我认为 ObservableList 可以解决问题,但似乎还有更多。你能给我一个解决方案吗?

控制器class:

package controller;

import java.lang.reflect.Constructor;

import db.ItemLijst;
import db.Klant;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import model.Item;
import model.ItemTypes;

public class SchermController {
    @FXML
    private TableView<Item> tblItems;
    @FXML
    private TableView<Klant> tblKlanten;
//  @FXML
//  private TableView<Uitlening> tblUitleningen;
    @FXML
    private Button btnItemToevoegen;
    @FXML
    private Button btnKlantToevoegen;
    @FXML
    private Button btnRegistreer;
    @FXML
    private ChoiceBox<ItemTypes> cbTypes;
    @FXML
    private ChoiceBox<Item> cbItems;
    @FXML
    private ChoiceBox<Klant> cbKlanten;
    @FXML
    private TextField tfTitel;
    @FXML
    private TextField tfVoornaam;
    @FXML
    private TextField tfAchternaam;
    @FXML
    private TextField tfStraat;
    @FXML
    private TextField tfNummer;
    @FXML
    private TextField tfPostcode;
    @FXML
    private TextField tfGemeente;
    @FXML
    private TableColumn<Item, String> tcID;
    @FXML
    private TableColumn<Item, ItemTypes> tcType;
    @FXML
    private TableColumn<Item, String> tcTitel;
    @FXML
    private TableColumn<Item, String> tcUitgeleend;
    private ObservableList<Item> items = FXCollections.observableArrayList();

    @FXML
    private void initialize()
    {
            /*
             * ItemLijst is a static object (or object with all static 
methods). the CD, Film and Spel classes automatically add their record to 
this class. Literally translated it means ItemList. I thought by making this 
an ObservableList, the table would be automatically update its records 
depending on a change to this variable (ItemLijst.items<Item>)?
             */
        items = FXCollections.observableArrayList(ItemLijst.getItems());

        tcID.setCellValueFactory(new PropertyValueFactory<Item, String>("ID"));
        tcType.setCellValueFactory(new PropertyValueFactory<Item, ItemTypes>("Type"));
        tcTitel.setCellValueFactory(new PropertyValueFactory<Item, String>("Titel"));
        tcUitgeleend.setCellValueFactory(new PropertyValueFactory<Item, String>("UitgeleendString"));
        tblItems.setItems(items);

        // Item types
        cbTypes.setItems(FXCollections.observableArrayList(ItemTypes.values()));

        btnItemToevoegen.setOnAction(e -> {
            try {
                itemToevoegen();
            } catch (Exception e1) {
                System.out.println("Probleem: " + e1.getMessage());
            }
        });
    }

/**
* This is the method which I use to add a new item to the CD, Film or Spel 
class (which inherits the Item class).

*/
    private void itemToevoegen() throws Exception
    {
        // Validatie
        if (cbTypes.getValue() == null ) {
            throw new Exception("Je moet een type kiezen");
        } else if (tfTitel.getText().trim().isEmpty()) {
            throw new Exception("Je moet een titel ingeven");
        }

        Class<?> klasse = Class.forName("model." + cbTypes.getValue().toString());
        Constructor<?> cons = klasse.getConstructor(String.class);
        cons.newInstance(tfTitel.getText());
    }
}

FXML:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="438.0" prefWidth="743.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.SchermController">
   <children>
      <TabPane layoutX="-1.0" prefHeight="438.0" prefWidth="744.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab closable="false" text="Items">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="367.0" prefWidth="766.0">
                     <children>
                        <TableView fx:id="tblItems" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
                          <columns>
                            <TableColumn fx:id="tcID" prefWidth="303.0" text="Item ID" />
                            <TableColumn fx:id="tcType" prefWidth="76.0" text="Type" />
                            <TableColumn fx:id="tcTitel" prefWidth="236.0" text="Titel" />
                              <TableColumn fx:id="tcUitgeleend" prefWidth="98.0" text="Uitgeleend" />
                          </columns>
                        </TableView>
                        <ChoiceBox fx:id="cbTypes" layoutX="23.0" layoutY="365.0" prefWidth="150.0" />
                        <TextField fx:id="tfTitel" layoutX="201.0" layoutY="365.0" prefHeight="26.0" prefWidth="352.0" promptText="Titel van item" />
                        <Button fx:id="btnItemToevoegen" layoutX="592.0" layoutY="365.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="131.0" text="Toevoegen" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab closable="false" text="Klanten">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tblKlanten" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
                           <columns>
                              <TableColumn prefWidth="89.0" text="Klant ID" />
                              <TableColumn prefWidth="87.0" text="Voornaam" />
                              <TableColumn prefWidth="105.0" text="Achternaam" />
                              <TableColumn prefWidth="154.0" text="Straat" />
                              <TableColumn prefWidth="54.0" text="Nr" />
                              <TableColumn prefWidth="96.0" text="Postcode" />
                              <TableColumn prefWidth="128.0" text="Gemeente" />
                           </columns>
                        </TableView>
                        <TextField fx:id="tfVoornaam" layoutX="14.0" layoutY="365.0" prefHeight="26.0" prefWidth="105.0" promptText="Voornaam" />
                        <TextField fx:id="tfAchternaam" layoutX="125.0" layoutY="365.0" prefHeight="26.0" prefWidth="94.0" promptText="Achternaam" />
                        <TextField fx:id="tfStraat" layoutX="243.0" layoutY="365.0" prefHeight="26.0" prefWidth="150.0" promptText="Straat" />
                        <TextField fx:id="tfNummer" layoutX="396.0" layoutY="365.0" prefHeight="26.0" prefWidth="32.0" promptText="Nr" />
                        <TextField fx:id="tfPostcode" layoutX="431.0" layoutY="365.0" prefHeight="26.0" prefWidth="60.0" promptText="Postcode" />
                        <TextField fx:id="tfGemeente" layoutX="494.0" layoutY="365.0" prefHeight="26.0" prefWidth="130.0" promptText="Gemeente" />
                        <Button fx:id="btnKlantToevoegen" layoutX="635.0" layoutY="365.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="93.0" text="Toevoegen" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab closable="false" text="Uitleningen">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tblUitleningen" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
                           <columns>
                              <TableColumn prefWidth="92.0" text="Klant ID" />
                              <TableColumn prefWidth="324.0" text="Item ID" />
                              <TableColumn prefWidth="155.0" text="Start Uitleen" />
                              <TableColumn prefWidth="142.0" text="Eind Uitleen" />
                           </columns>
                        </TableView>
                        <ChoiceBox fx:id="cbItems" layoutX="15.0" layoutY="364.0" prefHeight="26.0" prefWidth="185.0" />
                        <ChoiceBox fx:id="cbKlanten" layoutX="244.0" layoutY="364.0" prefHeight="26.0" prefWidth="203.0" />
                        <Button fx:id="btnRegistreer" layoutX="497.0" layoutY="364.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="229.0" text="Registreer Uitlening" />
                     </children>
                  </AnchorPane>
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

查看 JavaDoc 你会发现:

public static <E> ObservableList<E> observableArrayList(Collection<? extends E> col)

Creates a new observable array list and adds a content of collection col to it.

所以实际上,您正在创建一个新的 ObservableList 并向其中添加初始项目。最简单的解决方案是让 ItemsLijst 有一个 ObservableList,并且 return 它在 getItems 中。那么你只需要做

tblItems.setItems(ItemsLijst.getItems()); // I corrected the class name