Skip to content

Commit a272c4f

Browse files
Michael Straußkevinrushforth
Michael Strauß
authored andcommitted
8273324: IllegalArgumentException: fromIndex(0) > toIndex(-1) after clear and select TableCell
Reviewed-by: jpereda, kcr
1 parent 26d6438 commit a272c4f

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/ControlUtils.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,15 @@ static <T> ListChangeListener.Change<T> buildClearAndSelectChange(
8686
private int from = -1;
8787

8888
{
89-
int midIndex = -Collections.binarySearch(removed, retainedRow, rowComparator) - 1;
90-
firstRemovedRange = removed.subList(0, midIndex);
91-
secondRemovedRange = removed.subList(midIndex, removedSize);
89+
int insertionPoint = Collections.binarySearch(removed, retainedRow, rowComparator);
90+
if (insertionPoint >= 0) {
91+
firstRemovedRange = removed;
92+
secondRemovedRange = Collections.emptyList();
93+
} else {
94+
int midIndex = -insertionPoint - 1;
95+
firstRemovedRange = removed.subList(0, midIndex);
96+
secondRemovedRange = removed.subList(midIndex, removedSize);
97+
}
9298
}
9399

94100
@Override public int getFrom() {

modules/javafx.controls/src/test/java/test/javafx/scene/control/TableViewTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,50 @@ public class TableViewTest {
352352
assertEquals("Item 2", fm.getFocusedItem());
353353
}
354354

355+
@Test
356+
public void ensureRowRemainsSelectedWhenSelectingCellInSameRow() {
357+
class Person {
358+
final String firstName, lastName;
359+
Person(String firstName, String lastName) {
360+
this.firstName = firstName;
361+
this.lastName = lastName;
362+
}
363+
public String getFirstName() { return firstName; }
364+
public String getLastName() { return lastName; }
365+
}
366+
367+
var tableView = new TableView<Person>();
368+
tableView.getSelectionModel().setCellSelectionEnabled(true);
369+
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
370+
371+
var col1 = new TableColumn<Person, String>();
372+
col1.setCellValueFactory(new PropertyValueFactory<>("firstName"));
373+
var col2 = new TableColumn<Person, String>();
374+
col2.setCellValueFactory(new PropertyValueFactory<>("lastName"));
375+
tableView.getColumns().addAll(List.of(col1, col2));
376+
377+
var ab = new Person("a", "b");
378+
tableView.getItems().add(ab);
379+
var cd = new Person("c", "d");
380+
tableView.getItems().add(cd);
381+
382+
var selectionModel = tableView.getSelectionModel();
383+
selectionModel.select(0);
384+
selectionModel.clearAndSelect(0, col1);
385+
386+
// The following asserts should work once JDK-8273336 is fixed:
387+
//
388+
// assertEquals(1, selectionModel.getSelectedIndices().size());
389+
// assertEquals(0, (int)selectionModel.getSelectedIndices().get(0));
390+
391+
selectionModel.clearSelection();
392+
selectionModel.selectRange(0, col1, 1, col2);
393+
selectionModel.clearAndSelect(1, col2);
394+
395+
// assertEquals(1, selectionModel.getSelectedIndices().size());
396+
// assertEquals(1, (int)selectionModel.getSelectedIndices().get(0));
397+
}
398+
355399
/*********************************************************************
356400
* Tests for columns *
357401
********************************************************************/

0 commit comments

Comments
 (0)