【问题标题】:JavaFX TableView Not PopulatingJavaFX TableView 未填充
【发布时间】:2014-10-08 15:32:32
【问题描述】:

在 Java 8 中使用 JavaFx 2 和 Netbeans IDE 8.0。 我正在从FileMaker 数据库中提取数据,并将其作为“票证”对象添加到observableArrayList(此处称为“行”)。然后我将该列表传递给表格以显示已找到的内容。 我填充了observableArrayList,但我无法让它显示在表格中。

我的表格视图内置在 FXML 中:

<TableView fx:id="ticketData" layoutX="-3.0" layoutY="351.0" prefHeight="203.0" prefWidth="554.0">
                       <columns>
                          <TableColumn fx:id="ticket" editable="false" prefWidth="59.0" text="Ticket" />
                          <TableColumn fx:id="dates" prefWidth="72.0" text="Date(s)" />
                          <TableColumn fx:id="files" editable="false" prefWidth="114.0" text="File(s) Found" />
                          <TableColumn fx:id="notes" prefWidth="297.0" text="Notes" />
                       </columns>
                    </TableView>

我的 Java 类有我的 setter/getter 等:

    public  class Ticket 
    {
    private  SimpleStringProperty  ticketType;
    private  SimpleObjectProperty<Date>  executionDate;
    private  SimpleBooleanProperty gotFile;
    private  SimpleStringProperty  statusMessage;

    public Ticket(String tt, Date done, boolean gf, String stat)
    {
        this.ticketType = new SimpleStringProperty (tt);
        this.executionDate = new SimpleObjectProperty<Date>(done) {};
        this.gotFile = new SimpleBooleanProperty(gf);
        this.statusMessage = new SimpleStringProperty(stat);
    }


     public String getTicketType() {
        return ticketType.get();
    }
    public void setTicketType(String tt) {
        ticketProperty().set(tt);
    }
    public StringProperty ticketProperty() { 
         if (ticketType == null) ticketType = new SimpleStringProperty(this, "ticketType");
         return ticketType; 
     }

     public Date getExecutionDate() {
        return executionDate.get();
    }
    public void setExecutionDate(Date done) {
        executionDate.set(done);
    }
        public SimpleObjectProperty dateProperty() { 
         if (executionDate == null) executionDate = new SimpleObjectProperty(this, "executionDate");
         return executionDate; 
     }

    public boolean getGotFile(){
        return gotFile.get();
    }
    public void setGotFile(boolean gf){
        gotFile.set(gf);
    }
        public SimpleBooleanProperty gotProperty() { 
         if (gotFile == null) gotFile = new SimpleBooleanProperty(this, "gotFile");
         return gotFile; 
     }

    public String getStatusMessage() {
       return statusMessage.get();
   }
    public void setStatusMessage(String stat) {
        statusMessage.set(stat);
    }
        public StringProperty statusProperty() { 
         if (statusMessage == null) statusMessage = new SimpleStringProperty(this, "statusMessage");
         return statusMessage; 
     }
}

用于从FileMaker收集我需要的resultset数据的方法:

public void accumulate(String type, Date essential, boolean got, String msg)
    {
        Ticket ticket;
        ticket = new Ticket(type, essential, got, msg);
        row.add(ticket);
    }

完成对所需内容的搜索后,我在控制器中设置我的tableview(此处为“ticketData”)来保存项目,每列都有一个与票证相关的CellValueFactory类。

ticketData = new TableView();

           ticket.setCellFactory(TextFieldTableCell.forTableColumn());
        ticket.setCellValueFactory(
                new PropertyValueFactory<>("ticketType"));


           dates.setCellFactory(TextFieldTableCell.forTableColumn());
           dates.setCellValueFactory(
                new PropertyValueFactory<>("executionDate"));

           files.setCellFactory(TextFieldTableCell.forTableColumn());
           files.setCellValueFactory(
                new PropertyValueFactory<>("gotFile"));

           notes.setCellFactory(TextFieldTableCell.forTableColumn());
           notes.setCellValueFactory(
                new PropertyValueFactory<>("statusMessage"));

        ticketData.getColumns().addAll(ticket,dates,files,notes);
        //System.out.println(Arrays.deepToString(row.toArray()));

        ticketData.setItems(row);

我可以在observableArrayList 上打印并查看其中的条目,但tableview 仍为空白。

有什么建议吗?我错过了什么吗?我很茫然。

【问题讨论】:

标签: java javafx-2 tableview


【解决方案1】:

PropertyValueFactory&lt;&gt;("TicketType")应该是ticketType,注意小写。所有字段都一样。

http://docs.oracle.com/javafx/2/api/javafx/scene/control/cell/PropertyValueFactory.html

【讨论】:

  • 我原来是这样的,但没用。刚才把它改回来,看看它是否会有所作为,但什么也没有。我的表格视图仍然是空白的 :(
  • 也许您没有从数据库中获取数据。
  • 在数据进入 observableArrayList 之前以及将其传递给表视图之前,我已将其打印出来。那里肯定有数据,这是令人抓狂的部分。
  • ticketData = new TableView(); 不应该在那里。这就是为什么您需要一个 SSCE 供人们测试的原因。我看不到它在哪里声明,但在某处应该有一个@FXML TableView ticketData 声明。您正在制作一张新表格,而不是使用 fxml 中制作的表格。列和您使用的所有其他 fxml 构造都相同。
  • 我也对所有没有字符串转换器的TextFieldTableCell 有疑问,但这是另一个问题。
猜你喜欢
  • 2018-10-14
  • 2013-06-06
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多