简体   繁体   English

JavaFX:数据未显示在 TableView 中

[英]JavaFX: data doesn't show up in TableView

I'm trying to understand java more and im stuck at a FX problem.我试图更多地了解 java 并且我陷入了 FX 问题。 I have a simple 'create and object' and it will show in the TableView.我有一个简单的“创建和对象”,它将显示在 TableView 中。 Except it doesnt.除了它没有。 I've tried fixing the problem and still no success.我已经尝试解决问题,但仍然没有成功。 I just want to create an object (CakeRequest) and when I click the button, the request to show up in the TableView.我只想创建一个 object (CakeRequest),当我单击按钮时,请求会显示在 TableView 中。

HelloApplication.java HelloApplication.java


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;


public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 990, 600);
        stage.setTitle("Cake Request Simulator and all that! Tell your friends!");
        stage.setScene(scene);
        stage.show();
    }

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

HelloController.Java HelloController.Java

package com.example.__fx__homeassignment;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import Domain.*;
import Repository.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import java.net.URL;
import java.util.ResourceBundle;

public class HelloController {
    @FXML
    private TextField errorreport;
    @FXML
    private TextField errorreportform;
    @FXML
    private TextField field_cakedesign;
    @FXML
    private TextField field_cakeflavour;
    @FXML
    private TextField field_cakemodel;
    @FXML
    private DatePicker field_date;
    @FXML
    private TextField field_employee;
    @FXML
    private TextField field_formID;
    @FXML
    private TextField field_formcakeID;
    @FXML
    private TextField field_id;
    @FXML
    private TextField field_owneradress;
    @FXML
    private TextField field_ownername;
    @FXML
    private TextField field_phonenumber;
    @FXML
    private TextField field_price;
    @FXML
    private TextField field_services;
    @FXML
    private ProgressBar progressbar;
    @FXML
    private ProgressBar progressbar2;
    @FXML
    private TextField progressformreport;
    @FXML
    private TextField progressreport;
    @FXML
    private TableColumn<CakeRequest, Integer> table_ID;
    @FXML
    private TableColumn<CakeRequest, String> table_adress;
    @FXML
    private TableColumn<CakeRequest, String> table_design;
    @FXML
    private TableColumn<CakeRequest, String> table_flavour;
    @FXML
    private TableColumn<CakeRequest, String> table_model;
    @FXML
    private TableColumn<CakeRequest, String> table_name;
    @FXML
    private TableColumn<CakeRequest, String> table_phonenumber;
    @FXML
    private TableView<CakeRequest> table;

    CakeRequestFormRepository formrepo = new CakeRequestFormRepository();
    CakeRequestRepository cakerepo=new CakeRequestRepository();

    public void initialize(URL location, ResourceBundle resources){
        table_ID.setCellValueFactory(new PropertyValueFactory<>("ID"));
        table_name.setCellValueFactory(new PropertyValueFactory<>("OwnerName"));
        table_adress.setCellValueFactory(new PropertyValueFactory<>("OwnerAddress"));
        table_phonenumber.setCellValueFactory(new PropertyValueFactory<>("PhoneNumber"));
        table_flavour.setCellValueFactory(new PropertyValueFactory<>("Flavour"));
        table_design.setCellValueFactory(new PropertyValueFactory<>("Design"));
        table_model.setCellValueFactory(new PropertyValueFactory<>("Model"));
    }


    @FXML
    void clickedcakeformbutton(ActionEvent event) {
        //Another functionality, but it doesnt matter since it doesnt show in the tableview
    }

    @FXML
    void clickedcakerequestnutton(ActionEvent event) {
        try {
            int ID= Integer.parseInt(field_id.getText());
            String ownername = field_ownername.getText();
            String adress = field_owneradress.getText();
            String phonenumber = field_phonenumber.getText();
            String cakemodel = field_cakemodel.getText();
            String cakeflavour = field_cakeflavour.getText();
            String cakedesign = field_cakedesign.getText();
            CakeRequest cake=new CakeRequest(ID, ownername, adress, phonenumber, cakemodel, cakeflavour, cakedesign);
            cakerepo.add(cake);
            table.getItems().add(cake);
            progressreport.setVisible(true);
            progressbar.setProgress(1);
            progressreport.setText("Done!");
        } catch (NumberFormatException errors) {
            progressbar.setStyle("-fx-accent: red;");
            progressreport.setVisible(true);
            progressbar.setProgress(1);
            progressreport.setText("Error!");
            errorreport.setVisible(true);
            errorreport.setText("Errors: "+errors);
        }
    }
}

hello-view.fxml你好-view.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="588.0" prefWidth="989.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.__fx__homeassignment.HelloController">
   <children>
      <TabPane layoutX="-8.0" layoutY="-5.0" prefHeight="769.0" prefWidth="997.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Cake Request">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="299.0" prefWidth="970.0">
                     <children>
                        <Button fx:id="cakerequestbutton" layoutX="356.0" layoutY="419.0" mnemonicParsing="false" onAction="#clickedcakerequestnutton" text="Add this request">
                           <font>
                              <Font size="30.0" />
                           </font>
                        </Button>
                        <Label layoutX="146.0" layoutY="14.0" prefHeight="108.0" prefWidth="681.0" text="Create a cake requet" textAlignment="CENTER" underline="true">
                           <font>
                              <Font name="Serif Italic" size="80.0" />
                           </font>
                        </Label>
                        <TextField fx:id="field_ownername" layoutX="299.0" layoutY="153.0" />
                        <TextField fx:id="field_cakemodel" layoutX="678.0" layoutY="153.0" />
                        <TextField fx:id="field_owneradress" layoutX="299.0" layoutY="197.0" />
                        <TextField fx:id="field_phonenumber" layoutX="299.0" layoutY="240.0" />
                        <TextField fx:id="field_cakeflavour" layoutX="678.0" layoutY="197.0" />
                        <TextField fx:id="field_cakedesign" layoutX="678.0" layoutY="240.0" />
                        <Label layoutX="146.0" layoutY="151.0" prefHeight="30.0" prefWidth="153.0" text="Owner's name:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <Label layoutX="146.0" layoutY="194.0" prefHeight="30.0" prefWidth="163.0" text="Owner's adress:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <Label layoutX="146.0" layoutY="238.0" prefHeight="30.0" prefWidth="183.0" text="Phone number:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <Label layoutX="536.0" layoutY="151.0" prefHeight="30.0" prefWidth="183.0" text="Cake model:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <Label layoutX="536.0" layoutY="195.0" prefHeight="30.0" prefWidth="183.0" text="Cake flavour:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <Label layoutX="536.0" layoutY="238.0" prefHeight="30.0" prefWidth="183.0" text="Cake design:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <ProgressBar fx:id="progressbar" layoutX="102.0" layoutY="362.0" prefHeight="21.0" prefWidth="768.0" progress="0.0" />
                        <TextField fx:id="progressreport" alignment="CENTER" editable="false" layoutX="411.0" layoutY="383.0" visible="false" />
                        <TextField fx:id="errorreport" editable="false" layoutX="628.0" layoutY="396.0" prefHeight="142.0" prefWidth="333.0" visible="false" />
                        <Label layoutX="417.0" layoutY="278.0" prefHeight="30.0" prefWidth="31.0" text="ID:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <TextField fx:id="field_id" layoutX="448.0" layoutY="280.0" prefHeight="25.0" prefWidth="128.0" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Cake Request Form">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="252.0" prefWidth="200.0">
                     <children>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="509.0" prefWidth="989.0">
                           <children>
                              <Button fx:id="cakeformbutton" layoutX="356.0" layoutY="419.0" mnemonicParsing="false" onAction="#clickedcakeformbutton" text="Add this request">
                                 <font>
                                    <Font size="30.0" />
                                 </font>
                              </Button>
                              <Label layoutX="60.0" layoutY="14.0" prefHeight="108.0" prefWidth="852.0" text="Create a cake requet form" textAlignment="CENTER" underline="true">
                                 <font>
                                    <Font name="Serif Italic" size="80.0" />
                                 </font>
                              </Label>
                              <TextField fx:id="field_services" layoutX="184.0" layoutY="153.0" prefHeight="25.0" prefWidth="721.0" />
                              <TextField fx:id="field_price" layoutX="184.0" layoutY="196.0" />
                              <TextField fx:id="field_employee" layoutX="184.0" layoutY="240.0" />
                              <Label layoutX="60.0" layoutY="151.0" prefHeight="30.0" prefWidth="153.0" text="Services:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <Label layoutX="60.0" layoutY="193.0" prefHeight="30.0" prefWidth="183.0" text="Price:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <Label layoutX="60.0" layoutY="238.0" prefHeight="30.0" prefWidth="183.0" text="Employee:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <Label layoutX="614.0" layoutY="194.0" prefHeight="30.0" prefWidth="183.0" text="Date:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <ProgressBar fx:id="progressbar2" layoutX="102.0" layoutY="362.0" prefHeight="21.0" prefWidth="768.0" progress="0.0" />
                              <DatePicker fx:id="field_date" layoutX="696.0" layoutY="196.0" />
                              <TextField fx:id="progressformreport" alignment="CENTER" editable="false" layoutX="412.0" layoutY="383.0" visible="false" />
                              <Label layoutX="614.0" layoutY="240.0" prefHeight="30.0" prefWidth="183.0" text="form ID:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <TextField fx:id="field_formID" layoutX="697.0" layoutY="240.0" prefHeight="25.0" prefWidth="174.0" />
                              <Label layoutX="356.0" layoutY="286.0" prefHeight="30.0" prefWidth="183.0" text="Cake ID:">
                                 <font>
                                    <Font size="20.0" />
                                 </font>
                              </Label>
                              <TextField fx:id="field_formcakeID" layoutX="440.0" layoutY="288.0" prefHeight="25.0" prefWidth="174.0" />
                           </children>
                        </AnchorPane>
                        <TextField fx:id="errorreportform" editable="false" layoutX="638.0" layoutY="406.0" prefHeight="142.0" prefWidth="333.0" visible="false" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab text="Database">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="491.0" prefWidth="998.0">
                     <children>
                        <Label layoutX="14.0" layoutY="14.0" prefHeight="30.0" prefWidth="322.0" text="The current cake requests:">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                        <TableView fx:id="table" layoutX="14.0" layoutY="44.0" prefHeight="515.0" prefWidth="976.0">
                          <columns>
                            <TableColumn fx:id="table_ID" prefWidth="75.0" text="ID" />
                            <TableColumn fx:id="table_name" prefWidth="183.0" text="Owner's name" />
                              <TableColumn minWidth="0.0" prefWidth="0.0" text="C1" />
                              <TableColumn fx:id="table_adress" prefWidth="183.0" text="Owner's adress" />
                              <TableColumn fx:id="table_phonenumber" prefWidth="137.0" text="Phone Number" />
                              <TableColumn fx:id="table_model" prefWidth="137.0" text="Model" />
                              <TableColumn fx:id="table_flavour" prefWidth="137.0" text="Flavour" />
                              <TableColumn fx:id="table_design" prefWidth="123.0" text="Design" />
                          </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

CakeRequest.java CakeRequest.java

public class CakeRequest implements Identifiable<Integer>, Serializable{
    private int ID;
    private String OwnerName;
    private String OwnerAddress;
    private String PhoneNumber;
    private String Model;
    private String Flavour;
    private String Design;
   
        public Integer getID() {
        return ID;
    }
    public String getOwnerName() {
        return OwnerName;
    }
    public String getOwnerAddress() {
        return OwnerAddress;
    }
    public String getPhoneNumber() {
        return PhoneNumber;
    }
    public String getFlavour() {
        return Flavour;
    }
    public String getDesign() {
        return Design;
    }
    public String getModel() {
        return Model;
    }

I've looked online, tried implementing fixes, but still nothing.我在网上查看,尝试实施修复,但仍然没有。 Any help?有什么帮助吗?

Error: PropertyValueFactory naming错误:PropertyValueFactory 命名

I can't reclose this because it was reopened by me, so I'll just replace the comments with an answer that adds some explanation and links to all of the duplicates.我无法重新关闭它,因为它是由我重新打开的,所以我将用一个答案替换评论,添加一些解释和指向所有重复项的链接。

Your names in your PropertyValueFactories are all wrong, see the duplicate:您在 PropertyValueFactories 中的名称都是错误的,请参阅副本:

For info on how to determine appropriate names.有关如何确定适当名称的信息。 But better yet, replace the PropertyValueFactories with lambdas, as in this example:但更好的是,将 PropertyValueFactories 替换为 lambda,如下例所示:

Error: initialize method not called错误:未调用初始化方法

Your initialize was not being called.您的初始化没有被调用。 See:看:

The fix to allow it to work (in addition to fixing the PropertyValueFactory names as you already did in an edit) is to remove all parameters from the initialize method.允许它工作的修复(除了像您在编辑中所做的那样修复 PropertyValueFactory 名称)是从初始化方法中删除所有参数。

You don't use them and, if you did, they can be injected via @FXML notation, see:你不使用它们,如果你这样做了,它们可以通过@FXML 表示法注入,请参阅:

I tested your app (after removing the domain stuff) and it worked (well at least for the portions that are implemented) and populated the table columns from the fields I was able to fill in (which was about half of them).我测试了您的应用程序(在删除域内容之后)并且它工作正常(至少对于已实现的部分)并从我能够填写的字段中填充表列(大约是其中的一半)。

Things that are not errors, but should be fixed不是错误但应该修复的事情

Follow Java naming conventions and apply them to every identifier you ever write in Java (especially important which using something like PropertyValueFactory which relies on those conventions, but most important because it is what other's expect when they read your code).遵循Java 命名约定并将它们应用于您在 Java 中编写的每个标识符(尤其重要的是使用依赖于这些约定的 PropertyValueFactory 之类的东西,但最重要的是因为这是其他人在阅读您的代码时所期望的)。

Also, fix spelling errors like nutton for button .此外,修复buttonnutton等拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM