Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8251941

ListCell: visual artifact when items contain null values

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • jfx16
    • jfx14
    • javafx

    Description

      To reproduce, compile and run the example - a list with a null item at index 0:

      - press remove null
      - press add null
      - increase window height a bit to show an additional row
      - expected: all rows beyond items.size are empty
      - actual: one of them rows shows "null"

      The culprit is incomplete logic in updateItem(int):

      if (valid) { // index in range
           ...
           updateItem(newItem, false)
      } else {
          if (!isEmpty
              && oldItem != null) {// this lets cell with null content slip through
                updateItem(null, true);
          }

      Fix seems to be to just remove the null check - off range cells must be marked empty always. Doing so leads to example behaving as expected, all tests still passing. Couldn't come up (yet) with a failing/passing unit test focused on this.

      The same incorrect logic is used in all types of cells, might have similar issues (didn't try, though)

      The example:

      public class ListCellNullValue extends Application {
          private Parent createContent() {
              // instantiate list containing null item
              ListView<String> list = new ListView<>(FXCollections.observableArrayList((String)null, "item 1"));
              
              Button removeNull = new Button("remove null");
              removeNull.setOnAction(e -> {
                  list.getItems().remove(0);
              });
              
              Button addNull = new Button("add null");
              addNull.setOnAction(e -> {
                  list.getItems().add(0, null);
              });
              BorderPane content = new BorderPane(list);
              content.setBottom(new HBox(10, removeNull, addNull));
              return content;
          }

          @Override
          public void start(Stage stage) throws Exception {
              stage.setScene(new Scene(createContent()));
              stage.setTitle(FXUtils.version());
              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }

      Attachments

        Issue Links

          Activity

            People

              fastegal Jeanette Winzenburg
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: