0

I know there are many questions about this, but the answers didn't solve my problem. I have a TableView with 4 columns. The data is read out of a txt file.
In the initialize method from the Controller the CellValueFactory is set, and another method is called, which reads the Data from the file and creates an observableList of the model. Then the list is added to the items of the table, but no data is displayed, and no placeholder is there. Does anyone know what is wrong?

Controller class

@FXML
private TableView<Data> dataTable;
@FXML
private TableColumn<Data, String> dataType;
@FXML
private TableColumn<Data, String> dataName;
@FXML
private TableColumn<Data, String> dataDate;
@FXML
private TableColumn<Data, String> dataInformation;


@FXML
public void initialize() {
    dataType.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataType()));
    dataName.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataName()));
    dataDate.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataDate()));
    dataInformation.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataInformation()));
    initData();
}

private void initData() {
    ArrayList<Data> data = new ArrayList<>();
    try {
        Path filePath = Paths.get("src/main/resources/kl/fla/decrypted.txt");
        if (filePath.toFile().exists()) {
            data = (ArrayList<Data>) Files.readAllLines(filePath, StandardCharsets.UTF_8).stream()
                    .map(Data::new).collect(Collectors.toList());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    dataTable.setItems(FXCollections.observableArrayList(data));
    dataTable.refresh();
}

FXML File

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" 
      prefWidth="1080.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="kl.fla.controller.TableController"
>
   <children>
      <Label alignment="CENTER" layoutX="393.0" layoutY="34.0" prefHeight="70.0" prefWidth="295.0" text="Hidden Gate">
         <font> 
            <Font size="48.0" />
         </font>
      </Label>
      <TableView fx:id="dataTable" fixedCellSize="1.0" layoutX="36.0" layoutY="141.0" prefHeight="439.0" prefWidth="840.0">
        <columns>
          <TableColumn fx:id="dataType" prefWidth="135.0" resizable="false" text="Datatype" />
          <TableColumn fx:id="dataName" minWidth="4.0" prefWidth="135.0" resizable="false" text="Name" />
            <TableColumn fx:id="dataDate" prefWidth="135.0" resizable="false" text="Date" />
            <TableColumn fx:id="dataInformation" minWidth="0.0" prefWidth="285.0" resizable="false" text="Information" />
        </columns>
      </TableView>
   </children>
</Pane>

Screenshot of the empty TableView

Screenshot TableView

spinnning
  • 35
  • 6
  • 2
    Does the data appear in the `List`? – trashgod Nov 01 '22 at 23:41
  • 2
    Calling `refresh()` on the table is not required (it is also not the cause of your issue). – jewelsea Nov 01 '22 at 23:49
  • 1
    `Paths.get("src/main/resources/kl/fla/decrypted.txt")` and using a file API, is not how you should [lookup resources](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). – jewelsea Nov 01 '22 at 23:52
  • 1
    Please provide the code for the `Data` class and a sample data file. – jewelsea Nov 01 '22 at 23:54
  • Try calling `dataTable.getItems().addAll(...)` instead of `dataTable.setItems(...)`. I think your problem comes from here because your table binds values with a data list which has been replaced by the list you've created; in other words, it binds to an empty list. But I need to be sure of that – 0009laH Nov 02 '22 at 08:48
  • 1
    @0009laH hmm .. what do you mean by _binds values with the data list_? don't see any such thingy. Populating the data as done here should simply work, doesn't matter whether addAll/setAll/setItems is used – kleopatra Nov 02 '22 at 10:38
  • @kleopatra To be more precise, "your table binds the element `items` to an observable list which has been replaced by the list you have created". But the problem was elsewhere :) – 0009laH Nov 02 '22 at 17:18
  • @0009laH _your table binds the element items to an observable list_ where exactly? I don't see anything like this, it's just replacing the value of the itemsProperty which is perfectly okay – kleopatra Nov 02 '22 at 17:48
  • @kleopatra Nowhere in this code, it was the default observable list which is instantiated with the table view. – 0009laH Nov 03 '22 at 13:37
  • 1
    @0009laH doesn't whether it's the default or a replaced observable list - itemsProperty is mutable, table must (and does) cope with replacing it – kleopatra Nov 03 '22 at 14:07

1 Answers1

3

The data are (probably) there; you just can’t see them because you have constrained every row to be just one pixel high:

fixedCellSize="1.0"

Remove that attribute and just use the default setting, and it should work (assuming there are no other errors in code you haven’t posted).

James_D
  • 201,275
  • 16
  • 291
  • 322
  • arrgg .. everything that possibly _can_ go wrong indeed _will_ go wrong sooner or later (and all bubbling up here at SO) - well spotted, eagle eye :) – kleopatra Nov 02 '22 at 10:42