Skip to content

Commit 44d53ba

Browse files
committed
8186188: TableColumHeader: initial auto-size broken if has graphic
Reviewed-by: angorya, rlichten
1 parent 0eb4d71 commit 44d53ba

File tree

4 files changed

+511
-10
lines changed

4 files changed

+511
-10
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, 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,10 +682,8 @@ private <T,S> void resizeColumnToFitContent(TableView<T> tv, TableColumn<T, S> t
682682
// RT-36855 - take into account the column header text / graphic widths.
683683
// Magic 10 is to allow for sort arrow to appear without text truncation.
684684
TableColumnHeader header = tableSkin.getTableHeaderRow().getColumnHeaderFor(tc);
685-
double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
686-
Node graphic = header.label.getGraphic();
687-
double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();
688-
double headerWidth = headerTextWidth + headerGraphicWidth + 10 + header.snappedLeftInset() + header.snappedRightInset();
685+
header.applyCss();
686+
double headerWidth = header.snappedLeftInset() + header.snappedRightInset() + header.label.prefWidth(-1) + 10;
689687
maxWidth = Math.max(maxWidth, headerWidth);
690688

691689
// RT-23486
@@ -780,10 +778,8 @@ private <T,S> void resizeColumnToFitContent(TreeTableView<T> ttv, TreeTableColum
780778
// RT-36855 - take into account the column header text / graphic widths.
781779
// Magic 10 is to allow for sort arrow to appear without text truncation.
782780
TableColumnHeader header = tableSkin.getTableHeaderRow().getColumnHeaderFor(tc);
783-
double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
784-
Node graphic = header.label.getGraphic();
785-
double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();
786-
double headerWidth = headerTextWidth + headerGraphicWidth + 10 + header.snappedLeftInset() + header.snappedRightInset();
781+
header.applyCss();
782+
double headerWidth = header.snappedLeftInset() + header.snappedRightInset() + header.label.prefWidth(-1) + 10;
787783
maxWidth = Math.max(maxWidth, headerWidth);
788784

789785
// RT-23486

modules/javafx.controls/src/shims/java/javafx/scene/control/skin/TableColumnHeaderShim.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, 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
@@ -24,6 +24,7 @@
2424
*/
2525
package javafx.scene.control.skin;
2626

27+
import javafx.scene.control.Label;
2728
import javafx.scene.control.TableColumn;
2829
import javafx.scene.control.TableColumnBase;
2930

@@ -64,4 +65,8 @@ public static void columnReorderingComplete(TableColumnHeader header) {
6465
public static void resizeColumnToFitContent(TableColumnHeader header, int nbRows) {
6566
header.resizeColumnToFitContent(nbRows);
6667
}
68+
69+
public static Label getLabel(TableColumnHeader tableColumnHeader) {
70+
return tableColumnHeader.label;
71+
}
6772
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/skin/TableColumnHeaderTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import javafx.collections.ObservableList;
3131
import javafx.event.Event;
3232
import javafx.scene.Node;
33+
import javafx.scene.control.ContentDisplay;
34+
import javafx.scene.control.Label;
3335
import javafx.scene.control.Skin;
3436
import javafx.scene.control.TableColumn;
37+
import javafx.scene.control.TableColumnBase;
3538
import javafx.scene.control.TableRow;
3639
import javafx.scene.control.TableView;
3740
import javafx.scene.control.cell.PropertyValueFactory;
@@ -40,6 +43,7 @@
4043
import javafx.scene.input.MouseEvent;
4144
import javafx.scene.layout.HBox;
4245

46+
import javafx.scene.text.Text;
4347
import org.junit.Before;
4448
import org.junit.After;
4549
import org.junit.Test;
@@ -281,6 +285,90 @@ public void test_resizeColumnToFitContentCustomRowSkin() {
281285
assertTrue(width > 0);
282286
}
283287

288+
/**
289+
* We expect that the css of the label is processed after the resizing took place,
290+
* since it is needed to correctly measure the size.
291+
*/
292+
@Test
293+
public void testResizeColumnToFitContentCssIsApplied() {
294+
Label label = TableColumnHeaderShim.getLabel(firstColumnHeader);
295+
label.setStyle("-fx-font-size: 24px;");
296+
firstColumnHeader.getTableColumn().setText("longlonglonglong");
297+
298+
assertEquals(12, label.getFont().getSize(), 0);
299+
300+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
301+
302+
assertEquals(24, label.getFont().getSize(), 0);
303+
}
304+
305+
/**
306+
* A table column with a graphic {@link Text} should be bigger than without.
307+
*/
308+
@Test
309+
public void testResizeColumnToFitContentWithGraphicText() {
310+
TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn();
311+
312+
tableColumn.setText("longlonglonglonglonglonglonglong");
313+
tableColumn.setGraphic(new Text("longlonglonglong"));
314+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
315+
316+
double widthWithGraphic = tableColumn.getWidth();
317+
318+
tableColumn.setGraphic(null);
319+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
320+
321+
double width = tableColumn.getWidth();
322+
323+
assertTrue(widthWithGraphic > width);
324+
}
325+
326+
/**
327+
* A table column with a graphic {@link Label} should be bigger than without.
328+
*/
329+
@Test
330+
public void testResizeColumnToFitContentWithGraphicLabel() {
331+
TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn();
332+
333+
tableColumn.setText("longlonglonglonglonglonglonglong");
334+
tableColumn.setGraphic(new Label("longlonglonglong"));
335+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
336+
337+
double widthWithGraphic = tableColumn.getWidth();
338+
339+
tableColumn.setGraphic(null);
340+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
341+
342+
double width = tableColumn.getWidth();
343+
344+
assertTrue(widthWithGraphic > width);
345+
}
346+
347+
/**
348+
* The content display should also be taken into consideration when measuring the width.
349+
* See also: <a href="https://bugs.openjdk.org/browse/JDK-8186188">JDK-8186188</a>
350+
*/
351+
@Test
352+
public void testResizeColumnToFitContentWithGraphicAlignment() {
353+
TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn();
354+
355+
tableColumn.setText("longlonglonglonglonglonglonglong");
356+
tableColumn.setGraphic(new Text("longlonglonglong"));
357+
358+
Label label = TableColumnHeaderShim.getLabel(firstColumnHeader);
359+
360+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
361+
362+
double widthWithGraphic = tableColumn.getWidth();
363+
364+
label.setContentDisplay(ContentDisplay.BOTTOM);
365+
TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1);
366+
367+
double width = tableColumn.getWidth();
368+
369+
assertTrue(widthWithGraphic > width);
370+
}
371+
284372
private TableRow<Person> createCustomRow(TableView<Person> tableView) {
285373
TableRow<Person> row = new TableRow<>() {
286374
protected Skin<?> createDefaultSkin() {

0 commit comments

Comments
 (0)