I am experiencing an intermittent issue in my JavaFX application where the casesTableView.getItems().clear()
method behaves inconsistently. Initially, it executes correctly, but on subsequent invocations, it sometimes runs twice or gets skipped entirely during the method execution.
The issue becomes apparent when loadCasesToTableView is called multiple times during the program’s runtime.
In my case:
- It runs correctly.
- The
casesTableView.getItems().clear()
line it runs twice. - The
casesTableView.getItems().clear()
line is not executed.
package hu.krisz.gui.model.physical;
import hu.krisz.gui.model.physical.managers.SelectionManager;
import hu.krisz.toshare.printerModel.PhysicalProperty;
import hu.krisz.toshare.printerModel.Printer;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.IntegerStringConverter;
public class TableViewLoader {
private Printer printer;
private TableView<String> propertiesTableView;
private TableColumn<String, String> colProperties;
private TableView<Integer> casesTableView;
private TableColumn<Integer, Integer> colCases;
public TableViewLoader(Printer printer, TableView<String> propertiesTableView, TableColumn<String, String> colProperties, TableView<Integer> casesTableView, TableColumn<Integer, Integer> colCases) {
this.printer = printer;
this.propertiesTableView = propertiesTableView;
this.colProperties = colProperties;
this.casesTableView = casesTableView;
this.colCases = colCases;
}
public void loadPropertiesToTableView(boolean selectLast, ObservableList<String> toPhysicalPropertySelection) {
propertiesTableView.getItems().clear();
colProperties.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue()));
colProperties.setCellFactory(TextFieldTableCell.forTableColumn());
for (PhysicalProperty prop : printer.getPhysicalProperties()) {
toPhysicalPropertySelection.add(prop.getName());
}
propertiesTableView.setItems(toPhysicalPropertySelection);
if (selectLast)
SelectionManager.selectLastElementOfTableView(propertiesTableView);
}
public void loadCasesToTableView(boolean selectLast, ObservableList<Integer> toCaseSelection) {
casesTableView.getItems().clear();
colCases.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue()));
colCases.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
int actuallyPropIndex = printer.whereIsThisProperty(SelectionManager.getSelectedPropertyName(propertiesTableView));
for (int i = 1; i <= printer.getPhysicalProperties().get(actuallyPropIndex).getMiningParameters().size(); i++) {
toCaseSelection.add(i);
}
casesTableView.setItems(toCaseSelection);
if (selectLast)
SelectionManager.selectLastElementOfTableView(casesTableView);
}
}
Unfortunately, this error could not be represented in a smaller project. How can I diagnose and resolve this inconsistent behavior?