JavaFX I can´t fill tableview by observableList


Question

i have problem with filling Tableview. When I run this code, my tableview has same number of records like Observablelist but nothing is visible.

Any idea? A donť understans some like cellvalueProperty. Is this usable for my example? Thanks.

 package SYSSEL;
 import java.sql.SQLException;
 import javafx.event.ActionEvent;
 import javafx.fxml.FXML;
 import javafx.scene.control.Button;
 import javafx.stage.Stage;
 import java.sql.ResultSet;
 import javafx.scene.control.*;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;

 public class gridOfMessages extends Stage {

   private ObservableList messages;
   @FXML
   private Button buttonCloseGrid;

   @FXML
   private TableColumn C1;

   @FXML
   private TableColumn C2;

   @FXML
   private TableColumn C3;


   @FXML
   private Button buttonRefreshGrid;

   @FXML
   private TableView<gridOfMessages> gridOfMessagesList;


   @FXML
   private void HandlerCloseGrid(ActionEvent event) {

   }

   @FXML
   private void handlerRefreshGrid(ActionEvent event) throws ClassNotFoundException, SQLException {
     //Main Scene
     ResultSet rq = PdfSorting.checkNewMsgs();
     rq.first();
     messages=FXCollections.observableArrayList();
     while(rq.next()){
       //Iterate Row;
       ObservableList<String> row = FXCollections.observableArrayList();
       row.add(rq.getString("ID_DOCUMENT_ESSL"));
       row.add(rq.getString("DOCUMENT_PATH"));
       row.add(rq.getString("BAN"));
       System.out.println("Row [1] added "+row );
       messages.add(row);

     }
     gridOfMessagesList.setItems(messages);

   }
 }



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

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" minWidth="307.0" prefHeight="506.999977929685" prefWidth="307.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="SYSSEL.gridOfMessages">
  <children>
    <Button id="ButtonCloseGrid" fx:id="buttonCloseGrid" layoutX="0.0" layoutY="468.0" mnemonicParsing="false" onAction="#HandlerCloseGrid" prefHeight="39.0" prefWidth="147.0" text="Zavřít" />
    <Button id="refreshGrid" fx:id="buttonRefreshGrid" layoutX="147.0" layoutY="468.0" mnemonicParsing="false" onAction="#handlerRefreshGrid" prefHeight="39.0" prefWidth="161.0" text="Aktualizovat" />
    <TableView fx:id="gridOfMessagesList" layoutX="0.0" layoutY="0.0" prefHeight="468.0" prefWidth="307.0">
      <columns>
        <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="72.0" text="Číslo datovky" fx:id="C1" />
        <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="147.0" text="Cesta" fx:id="C2" />
        <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="87.0" text="BAN" fx:id="C3" />
      </columns>
    </TableView>
  </children>
</AnchorPane>
1
1
10/14/2014 1:14:05 PM

Accepted Answer

Yes, you need to set cell value factory for every column in the table, that's for sure. But you should post your fxml file code too, without it I cannot confirm is it an issue here or not.

Cell value factory says from where value comes from. If your table data model type is gridOfMessages, then observable list for table data should be of the same type.

Edit:

From your fxml file is obvious that you need to add cell value factory for every column, something like in the following example, just replace firstName with property name from your data model bean.

  <TableColumn text="First Name">
    <cellValueFactory>
      <PropertyValueFactory property="firstName" />
    </cellValueFactory>
  </TableColumn>

If you don't set data type for TableView and ObservableList, default Object type will be assumed. Anyway, more than one column in the table is hard to make this way.

Edit:

Then you need to write a class which will represent one table row. For example, if you want to have table with three columns, you need a bean with three properties. Then set this as a type for TableView and ObservableList, populate that list with data, and set it for the table, similar as it is in your code already. That should be in the controller class. This is a general description, because I don't know what this table should represent.

You can help also with Oracle's tutorial about how to show data in the table with fxml.

0
10/14/2014 1:51:17 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon