繁体   English   中英

Java Fx Tableview 人口无法检索属性

[英]Java Fx Tableview population cannot retrieve property

我有一个表格视图,我通过按钮填充了信息,但是虽然我可以 select 保存数据的行,但文本不可见。

public class Account {
    private SimpleStringProperty Col_id, Col_name, Col_surname, Col_phone;
    
    public Account(String id, String name, String surname, String phone) {
        this.Col_id = new SimpleStringProperty(id);
        this.Col_name = new SimpleStringProperty(name);
        this.Col_surname = new SimpleStringProperty(surname);
        this.Col_phone = new SimpleStringProperty(phone);
    }

    public String getId() {
        return Col_id.get();
    }

    public void setId(String id) {
        this.Col_id.set(id);
    }

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

    public void setName(String name) {
        this.Col_name.set(name);
    }

    public String getSurname() {
        return Col_surname.get();
    }

    public void setSurname(String surname) {
        this.Col_surname.set(surname);
    }

    public String getNum() {
        return Col_phone.get();
    }

    public void setNum(String num) {
        this.Col_phone.set(num);
    }
}

这是我的 controller:

public class SampleController implements Initializable {
    
    
        @FXML
        private Button btnAccount, btnCustomer, btnBook, btnExit, btnAddPerson, btnAddBook, btnAddCustomer;
        @FXML
        private GridPane pnAccount, pnCustomer, pnBook;
        @FXML
        private Pane pnlStatus;
        @FXML
        private Label lblStatus, lblWelcome;    
        @FXML
        private ComboBox<String> boxCustomer, boxBook;
        @FXML
        private TextField txtName,txtSurname,txtPhone, txtBookName,txtAuthor,txtCustomerId,txtBookId;
        @FXML
        private DatePicker processDate;
        @FXML
        private TableView<Account> tablePerson;
        @FXML
        private TableColumn<Account, String> id;
        @FXML
        private TableColumn<Account, String> name;
        @FXML
        private TableColumn<Account, String> surname;
        @FXML
        private TableColumn<Account, String> phone;
    
        
        @FXML
        private TableView<Book> tableBook;
        
        @FXML
        private TableView<Customer> tableCustomer;
        
        @FXML
        private void handleClicks (ActionEvent event) {
        
        lblStatus.setVisible(true);
        pnlStatus.setVisible(true);
        lblWelcome.setVisible(false);
        
        if(event.getSource() == btnAccount){
            
            lblStatus.setText("Create Account");
            pnAccount.setVisible(true);
            pnBook.setVisible(false);
            pnCustomer.setVisible(false);
        }
    
        
    @FXML
    private void clickAddPerson(ActionEvent event) {
        event.consume();
        int id = Main.getRandomNumberUsingNextInt(000000, 999999);
        boolean check = true;
        while(check) {
            String s=String.valueOf(id);
            check = Main.checkAccountId(s);
            if(check == false) {
                Account account = new Account(s,txtName.getText(), txtSurname.getText(), txtPhone.getText());
                Main.accounts.add(account);
                tablePerson.getItems().add(account);
            }
        }

        
    }

还有我的 fxml

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1080.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
   <children>
      <VBox layoutX="-2.0" prefHeight="720.0" prefWidth="337.0" style="-fx-background-color: #3F2B63;">
         <children>
            <Pane prefHeight="207.0" prefWidth="337.0">
               <children>
                  <ImageView fitHeight="63.0" fitWidth="208.0" layoutX="137.0" layoutY="34.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@literature-xxl.png" />
                     </image>
                  </ImageView>
                  <Label layoutX="82.0" layoutY="104.0" text="Bookstore" textFill="WHITE">
                     <font>
                        <Font size="36.0" />
                     </font>
                  </Label>
               </children></Pane>
            <Button fx:id="btnAccount" alignment="BASELINE_LEFT" graphicTextGap="25.0" mnemonicParsing="false" onAction="#handleClicks" prefHeight="75.0" prefWidth="338.0" styleClass="sidebarItem" stylesheets="@application.css" text="Create Account" textFill="WHITE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font size="24.0" />
               </font>
               <graphic>
                  <ImageView fitHeight="23.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@add-user-xxl.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="btnCustomer" alignment="BASELINE_LEFT" graphicTextGap="25.0" mnemonicParsing="false" onAction="#handleClicks" prefHeight="75.0" prefWidth="337.0" styleClass="sidebarItem" stylesheets="@application.css" text="Transactions" textFill="WHITE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font size="24.0" />
               </font>
               <graphic>
                  <ImageView fitHeight="23.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@banknotes-xxl.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="btnBook" alignment="BASELINE_LEFT" graphicTextGap="25.0" mnemonicParsing="false" onAction="#handleClicks" prefHeight="75.0" prefWidth="337.0" styleClass="sidebarItem" stylesheets="@application.css" text="Book List" textFill="WHITE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font size="24.0" />
               </font>
               <graphic>
                  <ImageView fitHeight="23.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@books-xxl.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="btnExit" alignment="BASELINE_LEFT" graphicTextGap="25.0" mnemonicParsing="false" onAction="#handleClicks" prefHeight="75.0" prefWidth="337.0" styleClass="sidebarItem" stylesheets="@application.css" text="Exit" textFill="WHITE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font size="24.0" />
               </font>
               <graphic>
                  <ImageView fitHeight="23.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@close-window-xxl.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
         </children>
      </VBox>
      <StackPane layoutX="341.0" layoutY="336.0">
         <children>
            <GridPane fx:id="pnCustomer" prefHeight="345.0" prefWidth="691.0" visible="false">
               <columnConstraints>
                  <ColumnConstraints />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="300.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <TableView fx:id="tableCustomer" prefHeight="209.0" prefWidth="732.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <columns>
                        <TableColumn prefWidth="75.0" text="Customer ID" />
                        <TableColumn prefWidth="75.0" text="Book ID" />
                        <TableColumn prefWidth="75.0" text="Type" />
                        <TableColumn prefWidth="75.0" text="Date" />
                     </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
                  <HBox prefHeight="49.0" prefWidth="699.0" spacing="10.0" GridPane.columnIndex="1">
                     <children>
                        <TextField fx:id="txtCustomerId" prefHeight="27.0" prefWidth="237.0" promptText="Customer ID" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <TextField fx:id="txtBookId" prefHeight="27.0" prefWidth="249.0" promptText="Book ID" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <ComboBox fx:id="boxCustomer" prefWidth="150.0" promptText="Type" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <DatePicker fx:id="processDate" prefHeight="25.0" prefWidth="229.0" promptText="Date" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <Button fx:id="btnAddCustomer" mnemonicParsing="false" onAction="#clickAddCustomer" prefHeight="25.0" prefWidth="223.0" style="-fx-background-color: #3F2B63;" text="Approve" textFill="WHITE">
                           <graphic>
                              <ImageView fitHeight="12.0" fitWidth="11.0" pickOnBounds="true" preserveRatio="true">
                                 <image>
                                    <Image url="@plus-2-xxl.png" />
                                 </image>
                              </ImageView>
                           </graphic>
                        </Button>
                     </children>
                  </HBox>
               </children>
            </GridPane>
            <GridPane fx:id="pnBook" prefHeight="345.0" prefWidth="691.0" visible="false">
              <columnConstraints>
                <ColumnConstraints />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="300.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <TableView fx:id="tableBook" prefHeight="209.0" prefWidth="732.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
                    <columns>
                      <TableColumn prefWidth="75.0" text="ID" />
                      <TableColumn prefWidth="75.0" text="Book Name" />
                        <TableColumn prefWidth="75.0" text="Writer" />
                        <TableColumn prefWidth="75.0" text="Category" />
                        <TableColumn prefWidth="75.0" text="Stock" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
                  <HBox prefHeight="49.0" prefWidth="699.0" spacing="10.0" GridPane.columnIndex="1">
                     <children>
                        <TextField fx:id="txtBookName" prefHeight="27.0" prefWidth="408.0" promptText="Book Name" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <TextField fx:id="txtAuthor" prefHeight="27.0" prefWidth="245.0" promptText="Writer" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <ComboBox fx:id="boxBook" prefHeight="27.0" prefWidth="211.0" promptText="Category" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <Button fx:id="btnAddBook" mnemonicParsing="false" onAction="#clickAddBook" prefHeight="25.0" prefWidth="223.0" style="-fx-background-color: #3F2B63;" text="Add Book" textFill="WHITE">
                           <graphic>
                              <ImageView fitHeight="12.0" fitWidth="11.0" pickOnBounds="true" preserveRatio="true">
                                 <image>
                                    <Image url="@plus-2-xxl.png" />
                                 </image>
                              </ImageView>
                           </graphic>
                        </Button>
                     </children>
                  </HBox>
               </children>
            </GridPane>
            <GridPane fx:id="pnAccount" prefHeight="359.0" prefWidth="738.0" visible="false">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="300.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <TableView fx:id="tablePerson" prefHeight="154.0" prefWidth="743.0" GridPane.rowIndex="1">
                     <columns>
                        <TableColumn fx:id="id" prefWidth="75.0" text="ID" />
                        <TableColumn fx:id="name" prefWidth="75.0" text="Name" />
                        <TableColumn fx:id="surname" prefWidth="75.0" text="Surname" />
                        <TableColumn fx:id="phone" prefWidth="75.0" text="Phone Number" />
                     </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
                  <HBox prefHeight="103.0" prefWidth="718.0" spacing="10.0">
                     <children>
                        <TextField fx:id="txtName" prefHeight="27.0" prefWidth="206.0" promptText="Name" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <TextField fx:id="txtSurname" prefHeight="27.0" prefWidth="194.0" promptText="Surname" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <TextField fx:id="txtPhone" prefHeight="27.0" prefWidth="146.0" promptText="Phone" style="-fx-background-color: #fff; -fx-border-color: #3F2B63;" />
                        <Button fx:id="btnAddPerson" graphicTextGap="10.0" mnemonicParsing="false" onAction="#clickAddPerson" prefHeight="25.0" prefWidth="173.0" style="-fx-background-color: #3F2B63;" text="Add Account" textFill="WHITE">
                           <graphic>
                              <ImageView fitHeight="12.0" fitWidth="11.0" pickOnBounds="true" preserveRatio="true">
                                 <image>
                                    <Image url="@plus-2-xxl.png" />
                                 </image>
                              </ImageView>
                           </graphic>
                        </Button>
                     </children>
                  </HBox>
               </children>
            </GridPane>
         </children>
      </StackPane>
      <Pane fx:id="pnlStatus" layoutX="334.0" layoutY="107.0" prefHeight="166.0" prefWidth="746.0" style="-fx-background-color: #3F2B63;" visible="false">
         <children>
            <Label fx:id="lblStatus" layoutX="41.0" layoutY="46.0" prefHeight="75.0" prefWidth="415.0" text="Book List" textFill="WHITE">
               <font>
                  <Font size="48.0" />
               </font>
            </Label>
         </children>
      </Pane>
      <Label fx:id="lblWelcome" alignment="CENTER" layoutX="511.0" layoutY="305.0" prefHeight="63.0" prefWidth="398.0" style="-fx-background-color: #3F2B63;" text="Welcome to bookstore system" textFill="WHITE">
         <font>
            <Font size="24.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

正如我所说,我可以添加填写信息的帐户。 İt 填充线条。 但它是空白的。

似乎无法检索 Id, name, surname, phone 的属性有任何提示吗?^^

您必须为 controller 中的每一列设置 cellValueFactory(可能在初始化期间)。

如下所示:

id.setCellValueFactory(new PropertyValueFactory<Account, StringProperty>("id"));

我在reddit帖子上发现了问题。 在这里,如果修复任何人遇到同样的问题。

将此添加到模型信息.java

“打开应用程序到 fxml,javafx.base”

暂无
暂无

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

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