Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8251481: TableCell accessing row: NPE on auto-sizing
Reviewed-by: fastegal
  • Loading branch information
Marius Hanl authored and Jeanette Winzenburg committed Feb 2, 2022
1 parent ee52d14 commit 59cd8ec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -56,6 +56,7 @@
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumnBase;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
Expand Down Expand Up @@ -645,11 +646,17 @@ private <T,S> void resizeColumnToFitContent(TableView<T> tv, TableColumn<T, S> t
padding = r.snappedLeftInset() + r.snappedRightInset();
}

TableRow<T> tableRow = new TableRow<>();
tableRow.updateTableView(tv);

int rows = maxRows == -1 ? items.size() : Math.min(items.size(), maxRows);
double maxWidth = 0;
for (int row = 0; row < rows; row++) {
tableRow.updateIndex(row);

cell.updateTableColumn(tc);
cell.updateTableView(tv);
cell.updateTableRow(tableRow);
cell.updateIndex(row);

if ((cell.getText() != null && !cell.getText().isEmpty()) || cell.getGraphic() != null) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -341,6 +341,27 @@ private void test_rt36715_cellPropertiesMirrorTableColumnProperties(
cell.setSkin(new TableCellSkin(cell));
}

/**
* The {@link TableRow} should never be null inside the {@link TableCell} during auto sizing.
* Note: The auto sizing is triggered as soon as the table has a scene - so when the {@link StageLoader} is created.
* See also: JDK-8251481
*/
@Test
public void testRowIsNotNullWhenAutoSizing() {
TableColumn<String, String> tableColumn = new TableColumn<>();
tableColumn.setCellFactory(col -> new TableCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

assertNotNull(getTableRow());
}
});
table.getColumns().add(tableColumn);

stageLoader = new StageLoader(table);
}

/**
* Table: Editable<br>
* Row: Not editable<br>
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -682,6 +682,27 @@ private void test_rt36715_cellPropertiesMirrorTableColumnProperties(
cell.setSkin(new TreeTableCellSkin(cell));
}

/**
* The {@link TreeTableRow} should never be null inside the {@link TreeTableCell} during auto sizing.
* Note: The auto sizing is triggered as soon as the table has a scene - so when the {@link StageLoader} is created.
* See also: JDK-8251481
*/
@Test
public void testRowIsNotNullWhenAutoSizing() {
TreeTableColumn<String, String> treeTableColumn = new TreeTableColumn<>();
treeTableColumn.setCellFactory(col -> new TreeTableCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

assertNotNull(getTableRow());
}
});
tree.getColumns().add(treeTableColumn);

stageLoader = new StageLoader(tree);
}

/**
* Table: Editable<br>
* Row: Not editable<br>
Expand Down

1 comment on commit 59cd8ec

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.