JavaFX controls inside TableView behavior strange - bug raised

Bug raised https://bugs.openjdk.java.net/browse/JDK-8088542

When trying to add editable functionality to a JavaFx editable TableView using the following test code I wrote: -

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class Foo extends Application implements Runnable
{

  @Override
  public void start(Stage stage) 
  {
    final TableView table = new TableView();
    final TableColumn column = new TableColumn();
    table.setEditable(true);
    column.setEditable(true);

    table.setItems(FXCollections.observableArrayList());

    column.setCellFactory(cell -> new EditableComboBoxCell());
    column.setCellValueFactory(param -> new SimpleStringProperty("foo"));

    table.getColumns().add(column);

    // Simple thread loop to demonstrate adding items

    new Thread(() ->
    {
        try
        {
            while (true)
            {
                Thread.sleep(20000);
                Platform.runLater(() -> table.getItems().add("foo"));
            }

        } catch (InterruptedException e)
        {
        }
    }).start();

    Scene scene = new Scene(new StackPane(table));

    stage.setScene(scene);

    stage.show();

  }

  @Override
  public void run()
  {
    launch();
  }
}

      

where "EditableComboBoxCell": -

public class EditableComboBoxCell extends TableCell<String, String>
{

  private ComboBox<Object> comboBox;

  @Override
  public void startEdit()
  {
    super.startEdit();
    comboBox = new ComboBox<>();
    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    setGraphic(comboBox);
  }

  @Override
  public void cancelEdit()
  {
    super.cancelEdit();
    comboBox = null;
    setContentDisplay(ContentDisplay.TEXT_ONLY);
    setText(getItem());
  }

  @Override
  public void updateItem(final String item, final boolean empty)
  {
    super.updateItem(item, empty);
    if (empty)
    {
      setText(null);
      setGraphic(null);
    }
    else
    {
      if (isEditing())
      {
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(comboBox);
      }
      else
      {
        setContentDisplay(ContentDisplay.TEXT_ONLY);
        setText(getItem());
      }
    }
  }
}

      

I find that if items are dynamically added, every second, the combo box disappears when the next item is added. I tested this by adding a new item every few seconds. This code works great when JavaFx TableView contains static data.

I have observed this behavior in Java 8u45 and Java 8u60 (Early Access).

I looked around and found that there is a bug with items not updating in the table view, and that the fix is ​​to set the column to invisible and then see it again, however that did not fix the problem. Moreover, this fix appears to be resolved in 8u60 and I am still observing this behavior.

Is the JavaFx table view not good at handling displaying controls when the table view is constantly adding / deleting data? Is there work around?

+3


source to share





All Articles