11/*
2- * Copyright (c) 2010, 2025 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2010, 2026 , 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
5656import javafx .scene .Group ;
5757import javafx .scene .Node ;
5858import javafx .scene .Parent ;
59- import javafx .scene .Scene ;
6059import javafx .scene .control .Cell ;
6160import javafx .scene .control .IndexedCell ;
6261import javafx .scene .control .ScrollBar ;
@@ -1072,8 +1071,8 @@ void adjustPosition() {
10721071 // when we enter this method, the absoluteOffset and position are
10731072 // already determined. In case this method invokes other methods
10741073 // that may change either absoluteOffset or position, it is the
1075- // responsability of those methods to make sure both parameters are
1076- // changed in a consistent way .
1074+ // responsibility of those methods to make sure both parameters are
1075+ // changed consistently .
10771076 // For example, the recalculateEstimatedSize method also recalculates
10781077 // the absoluteOffset and position.
10791078
@@ -1587,7 +1586,6 @@ private boolean tryScrollOneCell(int targetIndex, boolean downOrRight) {
15871586 setCellIndex (cell , targetIndex );
15881587 resizeCell (cell );
15891588 setMaxPrefBreadth (Math .max (getMaxPrefBreadth (), getCellBreadth (cell )));
1590- cell .setVisible (true );
15911589 if (downOrRight ) {
15921590 cells .addLast (cell );
15931591 scrollPixels (getCellLength (cell ));
@@ -2159,7 +2157,6 @@ void addLeadingCells(int currentIndex, double startOffset) {
21592157 // Position the cell, and update the maxPrefBreadth variable as we go.
21602158 positionCell (cell , offset );
21612159 setMaxPrefBreadth (Math .max (getMaxPrefBreadth (), getCellBreadth (cell )));
2162- cell .setVisible (true );
21632160 --index ;
21642161 }
21652162
@@ -2244,7 +2241,6 @@ boolean addTrailingCells(boolean fillEmptyCells) {
22442241 setMaxPrefBreadth (Math .max (getMaxPrefBreadth (), getCellBreadth (cell )));
22452242
22462243 offset += getCellLength (cell );
2247- cell .setVisible (true );
22482244 ++index ;
22492245 }
22502246
@@ -2276,7 +2272,6 @@ boolean addTrailingCells(boolean fillEmptyCells) {
22762272 prospectiveEnd += cellLength ;
22772273 positionCell (cell , start );
22782274 setMaxPrefBreadth (Math .max (getMaxPrefBreadth (), getCellBreadth (cell )));
2279- cell .setVisible (true );
22802275 }
22812276
22822277 // The amount by which to translate the cells down
@@ -2817,7 +2812,6 @@ protected T getPrivateCell(int index) {
28172812 if (cell != null ) {
28182813 setCellIndex (cell , index );
28192814 resizeCell (cell );
2820- cell .setVisible (false );
28212815 sheetChildren .add (cell );
28222816 privateCells .add (cell );
28232817 }
@@ -2842,46 +2836,40 @@ private void addToPile(T cell) {
28422836 }
28432837
28442838 private void cleanPile () {
2845- boolean wasFocusOwner = false ;
2839+ boolean focusOwnerHasScene = focusOwnerHasScene () ;
28462840
2841+ // Remove all cells that are in the pile, but still inside the sheet.
28472842 for (int i = 0 , max = pile .size (); i < max ; i ++) {
28482843 T cell = pile .get (i );
2849- wasFocusOwner = wasFocusOwner || doesCellContainFocus (cell );
2850- cell .setVisible (false );
2844+ if (cell .getParent () != null ) {
2845+ sheetChildren .remove (cell );
2846+ }
28512847 }
28522848
2853- // Remove all cells that are in the pile and therefore not relevant anymore.
2854- if (sheetChildren .size () != cells .size ()) {
2855- sheetChildren .removeAll (pile );
2856- }
2849+ // If the focus owner has a scene but does not have one anymore
2850+ // after we removed all cells from the sheet that are in the pile,
2851+ // we know that indeed the cell or a child (like the graphic) had focus.
28572852
28582853 // Fix for JDK-8095710: Rather than have the cells do weird things with
28592854 // focus (in particular, have focus jump between cells), we return focus
28602855 // to the VirtualFlow itself.
2861- if (wasFocusOwner ) {
2862- requestFocus ();
2856+ if (focusOwnerHasScene ) {
2857+ if (!focusOwnerHasScene ()) {
2858+ requestFocus ();
2859+ }
28632860 }
28642861 }
28652862
2866- private boolean doesCellContainFocus (T c ) {
2867- Scene scene = c .getScene ();
2868- final Node focusOwner = scene == null ? null : scene .getFocusOwner ();
2869-
2870- if (focusOwner != null ) {
2871- if (c .equals (focusOwner )) {
2872- return true ;
2873- }
2863+ private boolean focusOwnerHasScene () {
2864+ if (getScene () == null ) {
2865+ return false ;
2866+ }
28742867
2875- Parent p = focusOwner .getParent ();
2876- while (p != null && ! (p instanceof VirtualFlow )) {
2877- if (c .equals (p )) {
2878- return true ;
2879- }
2880- p = p .getParent ();
2881- }
2868+ if (getScene ().getFocusOwner () == null ) {
2869+ return false ;
28822870 }
28832871
2884- return false ;
2872+ return getScene (). getFocusOwner (). getScene () != null ;
28852873 }
28862874
28872875 private double getPrefBreadth (double oppDimension ) {
0 commit comments