简体   繁体   中英

JavaFX: data doesn't show up in TableView

I'm trying to understand java more and im stuck at a FX problem. I have a simple 'create and object' and it will show in the 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.

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

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

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

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

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:

For info on how to determine appropriate names. But better yet, replace the PropertyValueFactories with lambdas, as in this example:

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.

You don't use them and, if you did, they can be injected via @FXML notation, see:

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).

Also, fix spelling errors like nutton for button .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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