【问题标题】:Java Fx Tableview population cannot retrieve propertyJavafx Tableview 人口无法检索属性
【发布时间】:2021-08-23 01:15:44
【问题描述】:

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

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);
    }
}

这是我的控制器:

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>

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

似乎无法检索Id、姓名、姓氏、电话的属性有什么提示吗?^^

【问题讨论】:

标签: java javafx tableview scenebuilder


【解决方案1】:

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

将此添加到 model-info.java

"打开应用到 f​​xml, javafx.base"

【讨论】:

  • 如果这是解决方案,那么您应该在运行时遇到错误。如果您向我们提供了该错误,则有人能够更快、更准确地帮助您。如果您以后提出其他问题,请确保您的问题符合How to Ask 页面提供的指导方针,并提供minimal reproducible example
【解决方案2】:

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

如下所示:

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

【讨论】:

  • 最好推荐 OP 让 Account 公开属性(通过典型的 JavaFX 风格的“属性获取器”),然后使用 lambda 实现单元格值工厂(例如 id.setCellValueFactory(data -&gt; data.getValue().idProperty()))。这样可以避免反射并提供编译时安全性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 2011-01-20
  • 2012-10-05
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多