即使打印数据,Javafx SQLite Tableview 也不会填充

Javafx SQLite Tableview not Populating even though printing data

使用 Apache Netbeans IDE11.3.Currently 即使我可以打印数据也无法填充表视图。

我正在构建一个 Operator GUI thing.Currently 试图用 Operator 数据填充 tableview 但无法做到。

控制器 Class :

public class OperatorenController implements Initializable {

    DatabaseController db = new DatabaseController();
    private ObservableList<Operatoren> OperatorenList;

    @FXML
    private TableView<Operatoren> Operatorentable = new TableView<>();
    @FXML
    private TableColumn<Operatoren, Integer> id = new TableColumn<>();
    @FXML
    private TableColumn<Operatoren, String> name = new TableColumn<>();
    @FXML
    private TableColumn<Operatoren, String> nachname = new TableColumn<>();
    @FXML
    private TableColumn<Operatoren, String> firmname = new TableColumn<>();


    @FXML
    private void handleDisplayTables(ActionEvent event) throws NullPointerException {
        try {Connection conn = db.ConnectDB();
            String dbliste = "SELECT * FROM [operatoren]";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(dbliste);
            OperatorenList = FXCollections.observableArrayList();

            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String nachname = rs.getString("nachname");
                String firmname = rs.getString("firmname");
                OperatorenList.add(new Operatoren(id, name, nachname,firmname));
                System.out.println(id + " " + name+" " + nachname+" " + firmname+" ");


            }
        } catch (Exception e) {
            System.out.println(e);
        }

        id.setCellValueFactory(new PropertyValueFactory<>("id"));
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        nachname.setCellValueFactory(new PropertyValueFactory<>("nachname"));
        firmname.setCellValueFactory(new PropertyValueFactory<>("firmname"));

        name.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Operatoren)cellData.getValue()).getName()));});
        nachname.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Operatoren)cellData.getValue()).getNachname()));});
        firmname.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Operatoren)cellData.getValue()).getFirmname()));});
        id.setCellValueFactory(cellData -> { return (new SimpleIntegerProperty(((Operatoren)cellData.getValue()).getId())).asObject();});

        Operatorentable.setItems(OperatorenList);
        Operatorentable.getColumns().addAll(id,name,nachname,firmname);
        Operatorentable.refresh();

    }

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

     }
}

我可以从数据库打印数据,所以我不认为数据库是这里的问题。

运算符javaclass

public class Operatoren {
    public SimpleStringProperty name,nachname,firmname;
    public SimpleIntegerProperty id;

public Operatoren(int id,String name,String nachname,String firmname){
    this.id = new SimpleIntegerProperty(id);
    this.name = new SimpleStringProperty(name);
    this.nachname = new SimpleStringProperty(nachname);
    this.firmname = new SimpleStringProperty(firmname);

}
    public static void main(String[] args) {

    }

    public String getName() {
        return name.get();
    }

    public SimpleStringProperty getNameProperty() {
        return name;
    }

    public String getNachname() {
        return nachname.get();
    }

    public SimpleStringProperty getNachnameProperty() {
        return nachname;
    }

    public String getFirmname() {
        return firmname.get();
    }

    public SimpleStringProperty getFirmnameProperty() {
        return firmname;
    }

    public int getId() {
        return id.get();
    }

    public SimpleIntegerProperty getIdProperty() {
        return id;
    }
}

最后是 FXML,我正在使用 SceneBuilder :


<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="800.0" style="-fx-background-color: #313455;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="inf202.OperatorenController">
   <children>
      <Button layoutX="140.0" layoutY="235.0" mnemonicParsing="false" onAction="#personEkle" prefHeight="25.0" prefWidth="109.0" style="-fx-background-color: #f5f5f5;" text="Person ekle" />
      <Button layoutX="136.0" layoutY="354.0" mnemonicParsing="false" onAction="#personSil" prefHeight="25.0" prefWidth="109.0" style="-fx-background-color: #f5f5f5;" text="person sil" />
      <Button layoutX="136.0" layoutY="443.0" mnemonicParsing="false" onAction="#personList" prefHeight="25.0" prefWidth="109.0" style="-fx-background-color: #f5f5f5;" text="person listele" />
      <Button layoutX="136.0" layoutY="400.0" mnemonicParsing="false" onAction="#handleDisplayTables" prefHeight="25.0" prefWidth="109.0" style="-fx-background-color: #f5f5f5;" text="Person duzenle" />
      <Text fill="#948f8f" layoutX="122.0" layoutY="87.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Go Back">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <Line endX="84.0" endY="11.0" layoutX="25.0" layoutY="57.0" startX="68.0" startY="23.0" stroke="#948f8f" strokeWidth="2.0" />
      <Line endX="-85.0" endY="12.0" fill="#868686" layoutX="195.0" layoutY="81.0" startX="-103.4141845703125" startY="0.414215087890625" stroke="#948f8f" strokeWidth="2.0" />
      <Button layoutX="86.0" layoutY="68.0" mnemonicParsing="false" onAction="#Operatoren" opacity="0.0" prefHeight="25.0" prefWidth="109.0" text="Button" />
      <TextField layoutX="121.0" layoutY="126.0" />
      <TextField layoutX="121.0" layoutY="165.0" />
      <TextField layoutX="121.0" layoutY="200.0" />
      <Text fill="#948f8f" layoutX="61.0" layoutY="145.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Name">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <Text fill="#948f8f" layoutX="20.0" layoutY="184.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Nachname">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <Text fill="#948f8f" layoutX="21.0" layoutY="220.0" strokeType="OUTSIDE" strokeWidth="0.0" text="FirmName">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <TextField layoutX="121.0" layoutY="316.0" />
      <Text fill="#948f8f" layoutX="77.0" layoutY="335.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ID">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <Line endX="22.0" endY="249.0" layoutX="350.0" layoutY="49.0" startX="-323.0" startY="249.0" />
      <Line endX="-100.5" endY="231.5" layoutX="473.0" layoutY="67.0" startX="-100.5" startY="-50.0" />
      <Line endX="-102.25" endY="-188.79290771484375" layoutX="475.0" layoutY="488.0" startX="-102.25" startY="99.0" />
      <TextArea layoutX="28.0" layoutY="507.0" prefHeight="69.0" prefWidth="325.0" />
      <Text fill="#948f8f" layoutX="152.0" layoutY="494.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Ergebnis" wrappingWidth="77.1845703125">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Text>
      <TableView layoutX="389.0" layoutY="35.0" prefHeight="543.0" prefWidth="379.0">
         <columns>
            <TableColumn prefWidth="75.0" text="ID" />
            <TableColumn prefWidth="75.0" text="Name" />
            <TableColumn prefWidth="75.0" text="Nachname" />
            <TableColumn prefWidth="75.0" text="Firmname" />
         </columns>
      </TableView>
   </children>
</AnchorPane>

切勿初始化带注释的字段 @FXML

如果您删除控制器中的初始化,您会得到空指针异常的原因是您忘记将 fx:id 放在 FXML 文件中的元素上。

你传递数据的TableView是controller中定义的,而不是FXML文件中定义的(并显示在场景中)。同样,设置单元格值工厂的 TableColumns 是您在控制器中创建的,而不是您在 FXML 文件中定义的。因此,FXML 文件中定义的 table 列没有单元格值工厂,因此无法显示任何数据。

所以替换

@FXML
private TableView<Operatoren> Operatorentable = new TableView<>();
@FXML
private TableColumn<Operatoren, Integer> id = new TableColumn<>();
@FXML
private TableColumn<Operatoren, String> name = new TableColumn<>();
@FXML
private TableColumn<Operatoren, String> nachname = new TableColumn<>();
@FXML
private TableColumn<Operatoren, String> firmname = new TableColumn<>();

@FXML
private TableView<Operatoren> Operatorentable ;
@FXML
private TableColumn<Operatoren, Integer> id ;
@FXML
private TableColumn<Operatoren, String> name ;
@FXML
private TableColumn<Operatoren, String> nachname ;
@FXML
private TableColumn<Operatoren, String> firmname ;

并在 FXML 文件中添加 fx:id 属性:

  <TableView fx:id="Operatorentable" layoutX="389.0" layoutY="35.0" prefHeight="543.0" prefWidth="379.0">
     <columns>
        <TableColumn fx:id="id" prefWidth="75.0" text="ID" />
        <TableColumn fx:id="name" prefWidth="75.0" text="Name" />
        <TableColumn fx:id="nachname" prefWidth="75.0" text="Nachname" />
        <TableColumn fx:id="firmname" prefWidth="75.0" text="Firmname" />
     </columns>
  </TableView>

请注意,您在模型 class 中的方法命名是非标准的:对于 属性 prop,get 方法应为 getProp(),set 方法应为是 setProp(),属性 访问器方法应该是 propProperty(),而不是 getPropProperty()。如果您使用 PropertyValueFactory class(我实际上并不推荐),这可能会使事情无法正常工作。

public class Operatoren {
    public SimpleStringProperty name,nachname,firmname;
    public SimpleIntegerProperty id;

public Operatoren(int id,String name,String nachname,String firmname){
    this.id = new SimpleIntegerProperty(id);
    this.name = new SimpleStringProperty(name);
    this.nachname = new SimpleStringProperty(nachname);
    this.firmname = new SimpleStringProperty(firmname);

}
    public static void main(String[] args) {

    }

    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public String getNachname() {
        return nachname.get();
    }

    public SimpleStringProperty nachnameProperty() {
        return nachname;
    }

    public String getFirmname() {
        return firmname.get();
    }

    public SimpleStringProperty firmnameProperty() {
        return firmname;
    }

    public int getId() {
        return id.get();
    }

    public SimpleIntegerProperty idProperty() {
        return id;
    }
}

设置单元格值工厂并立即用本质上等效的工厂替换它们也没有意义,您应该在初始化时执行此操作,而不是在事件处理程序中执行。最后,由于您在模型中定义了 JavaFX 属性 class,因此无需为单元工厂创建新属性,只需引用现有属性即可:

@FXML
private void handleDisplayTables(ActionEvent event) throws NullPointerException {
    try {Connection conn = db.ConnectDB();
        String dbliste = "SELECT * FROM [operatoren]";
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(dbliste);
        OperatorenList = FXCollections.observableArrayList();

        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String nachname = rs.getString("nachname");
            String firmname = rs.getString("firmname");
            OperatorenList.add(new Operatoren(id, name, nachname,firmname));
            System.out.println(id + " " + name+" " + nachname+" " + firmname+" ");


        }
    } catch (Exception e) {
        System.out.println(e);
    }


    Operatorentable.setItems(OperatorenList);

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
    nachname.setCellValueFactory(cellData -> cellData.getValue().nachnameProperty());
    firmname.setCellValueFactory(cellData -> cellData.getValue().firmnameProperty());
    id.setCellValueFactory(cellData -> cellData.getValue().idProperty().asObject());


 }