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

ListView.EditEvent.getIndex() does not return the correct index

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      other

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      ListView.setOnEditCancel is called with an EventHandler, that gets a ListView.EditEvent as argument.

      That EditEvent has a function called `getIndex()`, which just returns the ListView's currently edited index, while the doc says, it would give the index of the edited field.

      If `onEditCancle` gets invoked because you edit a cell and cancel that by editing another field, EditEvent.getIndex() actually returns the index of the field, that cancelled the edit, and not the field that got cancelled

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the provided source code and click the buttons.

      Since this is a JavaFX problem, I couldn't quite figure out how to make a test case out of it. Sorry.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The output displays the index of the field, that had editing cancelled
      ACTUAL -
      The output displays the index of the field, that cancelled editing for the other

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package application;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ListView;
      import javafx.scene.control.cell.TextFieldListCell;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.StringConverter;

      public class Main extends Application {

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

          @Override
          public void start(Stage primaryStage) throws Exception {
              final ObservableList<String> list = FXCollections.observableArrayList("Zero", "One", "Two", "Three");
              final VBox frame = new VBox();
              final HBox hbox = new HBox();
              final Button editButton0 = new Button("Edit 0");
              final Button editButton1 = new Button("Edit 1");
              final Button editButton2 = new Button("Edit 2");
              final Button editButton3 = new Button("Edit 3");
              final ListView<String> listView = new ListView<>();

              //Setup listView
              listView.setItems(list);
              listView.setCellFactory(listViewCall -> new TextFieldListCell<>(new StringConverter<String>() {
                  @Override
                  public String toString(String object) {
                      return object;
                  }

                  @Override
                  public String fromString(String string) {
                      return string;
                  }
              }));
              listView.setEditable(true);

              //This is where the bug occurs
              listView.setOnEditCancel(event -> System.out.printf("Edit field (index %d) got cancelled\r\n", event.getIndex()));

              //Set each button to set one field to edit mode
              editButton0.setOnAction(event -> listView.edit(0));
              editButton1.setOnAction(event -> listView.edit(1));
              editButton2.setOnAction(event -> listView.edit(2));
              editButton3.setOnAction(event -> listView.edit(3));

              //Put everything into view
              hbox.getChildren().addAll(editButton0, editButton1, editButton2, editButton3);
              frame.getChildren().addAll(listView, hbox);

              primaryStage.setScene(new Scene(frame));
              primaryStage.show();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      call `listView.getSelectionModel().select(index)` before `listView.edit(index)`

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              fastegal Jeanette Winzenburg
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: