TabbedPane: fixed IndexOutOfBoundsException when using tooltip text on close buttons and closing last/rightmost tab (issue #235)

This commit is contained in:
Karl Tauber
2021-01-10 18:28:30 +01:00
parent 7d48bf06fe
commit 14222e40ad
3 changed files with 17 additions and 6 deletions

View File

@@ -21,6 +21,8 @@ FlatLaf Change Log
- Button and ToggleButton: Threat Unicode surrogate character pair as single - Button and ToggleButton: Threat Unicode surrogate character pair as single
character and make button square. (issue #234) character and make button square. (issue #234)
- TabbedPane: Fixed `IndexOutOfBoundsException` when using tooltip text on close
buttons and closing last/rightmost tab. (issue #235)
- Extras: Added missing export of package - Extras: Added missing export of package
`com.formdev.flatlaf.extras.components` to Java 9 module descriptor. `com.formdev.flatlaf.extras.components` to Java 9 module descriptor.
- JIDE Common Layer: Invoke `LookAndFeelFactory.installJideExtension()` when - JIDE Common Layer: Invoke `LookAndFeelFactory.installJideExtension()` when

View File

@@ -2109,8 +2109,10 @@ public class FlatTabbedPaneUI
public void mouseReleased( MouseEvent e ) { public void mouseReleased( MouseEvent e ) {
if( isPressedTabClose() ) { if( isPressedTabClose() ) {
updateRollover( e ); updateRollover( e );
if( pressedTabIndex >= 0 && pressedTabIndex == getRolloverTab() ) if( pressedTabIndex >= 0 && pressedTabIndex == getRolloverTab() ) {
restoreTabToolTip();
closeTab( pressedTabIndex ); closeTab( pressedTabIndex );
}
} else } else
mouseDelegate.mouseReleased( e ); mouseDelegate.mouseReleased( e );
@@ -2188,7 +2190,8 @@ public class FlatTabbedPaneUI
if( lastTipTabIndex < 0 ) if( lastTipTabIndex < 0 )
return; return;
tabPane.setToolTipTextAt( lastTipTabIndex, lastTip ); if( lastTipTabIndex < tabPane.getTabCount() )
tabPane.setToolTipTextAt( lastTipTabIndex, lastTip );
lastTip = null; lastTip = null;
lastTipTabIndex = -1; lastTipTabIndex = -1;
} }

View File

@@ -105,11 +105,11 @@ public class FlatContainerTest
} }
private void tabCountChanged() { private void tabCountChanged() {
for( JTabbedPane tabbedPane : allTabbedPanes ) for( FlatTabbedPane tabbedPane : allTabbedPanes )
tabCountChanged( tabbedPane ); tabCountChanged( tabbedPane );
} }
private void tabCountChanged( JTabbedPane tabbedPane ) { private void tabCountChanged( FlatTabbedPane tabbedPane ) {
int oldTabCount = tabbedPane.getTabCount(); int oldTabCount = tabbedPane.getTabCount();
int newTabCount = (Integer) tabCountSpinner.getValue(); int newTabCount = (Integer) tabCountSpinner.getValue();
@@ -126,7 +126,7 @@ public class FlatContainerTest
setTabIcons( tabbedPane ); setTabIcons( tabbedPane );
} }
private void addTab( JTabbedPane tabbedPane ) { private void addTab( FlatTabbedPane tabbedPane ) {
switch( tabbedPane.getTabCount() ) { switch( tabbedPane.getTabCount() ) {
case 0: case 0:
tabbedPane.addTab( "Tab 1", null, new Panel1(), "First tab." ); tabbedPane.addTab( "Tab 1", null, new Panel1(), "First tab." );
@@ -136,12 +136,14 @@ public class FlatContainerTest
JComponent tab2 = new Panel2(); JComponent tab2 = new Panel2();
tab2.setBorder( new LineBorder( Color.magenta ) ); tab2.setBorder( new LineBorder( Color.magenta ) );
tabbedPane.addTab( "Second Tab", null, tab2, "This is the second tab." ); tabbedPane.addTab( "Second Tab", null, tab2, "This is the second tab." );
tabbedPane.setTabCloseToolTipText( 1, "Close Second Tab" );
break; break;
case 2: case 2:
tabbedPane.addTab( "Disabled", createTab( "tab content 3" ) ); tabbedPane.addTab( "Disabled", createTab( "tab content 3" ) );
tabbedPane.setEnabledAt( 2, false ); tabbedPane.setEnabledAt( 2, false );
tabbedPane.setToolTipTextAt( 2, "Disabled tab." ); tabbedPane.setToolTipTextAt( 2, "Disabled tab." );
tabbedPane.setTabCloseToolTipText( 2, "Close Disabled tab" );
break; break;
case 3: case 3:
@@ -348,9 +350,13 @@ public class FlatContainerTest
if( closable ) { if( closable ) {
for( FlatTabbedPane tabbedPane : allTabbedPanes ) { for( FlatTabbedPane tabbedPane : allTabbedPanes ) {
tabbedPane.setTabCloseCallback( (tabbedPane2, tabIndex) -> { tabbedPane.setTabCloseCallback( (tabbedPane2, tabIndex) -> {
String tabTitle = tabbedPane2.getTitleAt( tabIndex );
AWTEvent e = EventQueue.getCurrentEvent(); AWTEvent e = EventQueue.getCurrentEvent();
int modifiers = (e instanceof MouseEvent) ? ((MouseEvent)e).getModifiers() : 0; int modifiers = (e instanceof MouseEvent) ? ((MouseEvent)e).getModifiers() : 0;
JOptionPane.showMessageDialog( this, "Closed tab '" + tabbedPane2.getTitleAt( tabIndex ) + "'."
tabbedPane2.removeTabAt( tabIndex );
JOptionPane.showMessageDialog( this, "Closed tab '" + tabTitle + "'."
+ "\n\n(modifiers: " + MouseEvent.getMouseModifiersText( modifiers ) + ")", + "\n\n(modifiers: " + MouseEvent.getMouseModifiersText( modifiers ) + ")",
"Tab Closed", JOptionPane.PLAIN_MESSAGE ); "Tab Closed", JOptionPane.PLAIN_MESSAGE );
} ); } );