0

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:

  1. It runs correctly.
  2. The casesTableView.getItems().clear() line it runs twice.
  3. 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?

2
  • 3
    Is the code of class TableViewLoader being executed on the JavaFX Application Thread ? Maybe consider posting a minimal reproducible example ? Commented Jul 28, 2024 at 11:52
  • 1
    I am not sure why you clear (casesTableView.getItems().clear()) and set (casesTableView.setItems(toCaseSelection)) the items, nor why you pass an ObservalbeList to loadCasesToTableView. I also don't know what is supposed to be in that list when you call the method. Why do you believe it is a synchronization issue or race condition? (there is no multi-threading in the code provided). Commented Jul 29, 2024 at 18:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.