TabbedPane: fixed NPE when using focusable component as tab component and switching theme (issue #745)

This commit is contained in:
Karl Tauber
2023-10-15 11:43:58 +02:00
parent 460b6492cb
commit 1dbe968952
2 changed files with 22 additions and 6 deletions

View File

@@ -9,6 +9,8 @@ FlatLaf Change Log
left/right vertical alignment and running in Java 19+. (issue #746) left/right vertical alignment and running in Java 19+. (issue #746)
- CheckBox and RadioButton: Fixed cut off right side when border is removed and - CheckBox and RadioButton: Fixed cut off right side when border is removed and
horizontal alignment is set to `right`. (issue #734) horizontal alignment is set to `right`. (issue #734)
- TabbedPane: Fixed NPE when using focusable component as tab component and
switching theme. (issue #745)
## 3.2.1 ## 3.2.1

View File

@@ -3567,7 +3567,7 @@ public class FlatTabbedPaneUI
//---- class FlatSelectedTabRepainter ------------------------------------- //---- class FlatSelectedTabRepainter -------------------------------------
private static class FlatSelectedTabRepainter private static class FlatSelectedTabRepainter
implements PropertyChangeListener//, Runnable implements PropertyChangeListener
{ {
private static FlatSelectedTabRepainter instance; private static FlatSelectedTabRepainter instance;
@@ -3617,17 +3617,31 @@ public class FlatTabbedPaneUI
break; break;
case "activeWindow": case "activeWindow":
repaintSelectedTabs( keyboardFocusManager.getPermanentFocusOwner() ); Component permanentFocusOwner = keyboardFocusManager.getPermanentFocusOwner();
if( permanentFocusOwner != null )
repaintSelectedTabs( permanentFocusOwner );
break; break;
} }
} }
private void repaintSelectedTabs( Component c ) { private void repaintSelectedTabs( Component c ) {
if( c instanceof JTabbedPane ) // Use invokeLater because this method may be invoked while UI update
repaintSelectedTab( (JTabbedPane) c ); // is in progress. This may happen if a focusable component (e.g. text field)
// is used as tab component (see JTabbedPane.setTabComponentAt()).
// uninstallTabContainer() removes all components from tabbed pane and
// the text field looses focus.
EventQueue.invokeLater( () -> {
// because this is invoked later, check whether component is still displayable
if( !c.isDisplayable() )
return;
while( (c = SwingUtilities.getAncestorOfClass( JTabbedPane.class, c )) != null ) if( c instanceof JTabbedPane )
repaintSelectedTab( (JTabbedPane) c ); repaintSelectedTab( (JTabbedPane) c );
Component c2 = c;
while( (c2 = SwingUtilities.getAncestorOfClass( JTabbedPane.class, c2 )) != null )
repaintSelectedTab( (JTabbedPane) c2 );
} );
} }
private void repaintSelectedTab( JTabbedPane tabbedPane ) { private void repaintSelectedTab( JTabbedPane tabbedPane ) {