Skip to content

Commit 59cd8ec

Browse files
Maran23Jeanette Winzenburg
authored andcommitted
8251481: TableCell accessing row: NPE on auto-sizing
Reviewed-by: fastegal
1 parent ee52d14 commit 59cd8ec

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/skin/TableColumnHeader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -56,6 +56,7 @@
5656
import javafx.scene.control.TableCell;
5757
import javafx.scene.control.TableColumn;
5858
import javafx.scene.control.TableColumnBase;
59+
import javafx.scene.control.TableRow;
5960
import javafx.scene.control.TableView;
6061
import javafx.scene.control.TreeTableCell;
6162
import javafx.scene.control.TreeTableColumn;
@@ -645,11 +646,17 @@ private <T,S> void resizeColumnToFitContent(TableView<T> tv, TableColumn<T, S> t
645646
padding = r.snappedLeftInset() + r.snappedRightInset();
646647
}
647648

649+
TableRow<T> tableRow = new TableRow<>();
650+
tableRow.updateTableView(tv);
651+
648652
int rows = maxRows == -1 ? items.size() : Math.min(items.size(), maxRows);
649653
double maxWidth = 0;
650654
for (int row = 0; row < rows; row++) {
655+
tableRow.updateIndex(row);
656+
651657
cell.updateTableColumn(tc);
652658
cell.updateTableView(tv);
659+
cell.updateTableRow(tableRow);
653660
cell.updateIndex(row);
654661

655662
if ((cell.getText() != null && !cell.getText().isEmpty()) || cell.getGraphic() != null) {

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -341,6 +341,27 @@ private void test_rt36715_cellPropertiesMirrorTableColumnProperties(
341341
cell.setSkin(new TableCellSkin(cell));
342342
}
343343

344+
/**
345+
* The {@link TableRow} should never be null inside the {@link TableCell} during auto sizing.
346+
* Note: The auto sizing is triggered as soon as the table has a scene - so when the {@link StageLoader} is created.
347+
* See also: JDK-8251481
348+
*/
349+
@Test
350+
public void testRowIsNotNullWhenAutoSizing() {
351+
TableColumn<String, String> tableColumn = new TableColumn<>();
352+
tableColumn.setCellFactory(col -> new TableCell<>() {
353+
@Override
354+
protected void updateItem(String item, boolean empty) {
355+
super.updateItem(item, empty);
356+
357+
assertNotNull(getTableRow());
358+
}
359+
});
360+
table.getColumns().add(tableColumn);
361+
362+
stageLoader = new StageLoader(table);
363+
}
364+
344365
/**
345366
* Table: Editable<br>
346367
* Row: Not editable<br>

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -682,6 +682,27 @@ private void test_rt36715_cellPropertiesMirrorTableColumnProperties(
682682
cell.setSkin(new TreeTableCellSkin(cell));
683683
}
684684

685+
/**
686+
* The {@link TreeTableRow} should never be null inside the {@link TreeTableCell} during auto sizing.
687+
* Note: The auto sizing is triggered as soon as the table has a scene - so when the {@link StageLoader} is created.
688+
* See also: JDK-8251481
689+
*/
690+
@Test
691+
public void testRowIsNotNullWhenAutoSizing() {
692+
TreeTableColumn<String, String> treeTableColumn = new TreeTableColumn<>();
693+
treeTableColumn.setCellFactory(col -> new TreeTableCell<>() {
694+
@Override
695+
protected void updateItem(String item, boolean empty) {
696+
super.updateItem(item, empty);
697+
698+
assertNotNull(getTableRow());
699+
}
700+
});
701+
tree.getColumns().add(treeTableColumn);
702+
703+
stageLoader = new StageLoader(tree);
704+
}
705+
685706
/**
686707
* Table: Editable<br>
687708
* Row: Not editable<br>

0 commit comments

Comments
 (0)