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>