mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 07:17:13 -06:00
TabbedPane: support specifying tooltip text for tab close buttons via client property
This commit is contained in:
@@ -280,6 +280,15 @@ public interface FlatClientProperties
|
|||||||
*/
|
*/
|
||||||
String TABBED_PANE_TAB_CLOSABLE = "JTabbedPane.tabClosable";
|
String TABBED_PANE_TAB_CLOSABLE = "JTabbedPane.tabClosable";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies the tooltip text used for tab close buttons.
|
||||||
|
* <p>
|
||||||
|
* <strong>Component</strong> {@link javax.swing.JTabbedPane}
|
||||||
|
* or tab content components (see {@link javax.swing.JTabbedPane#setComponentAt(int, java.awt.Component)})<br>
|
||||||
|
* <strong>Value type</strong> {@link java.lang.String}
|
||||||
|
*/
|
||||||
|
String TABBED_PANE_TAB_CLOSE_TOOLTIPTEXT = "JTabbedPane.tabCloseToolTipText";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies the callback that is invoked when a tab close button is clicked.
|
* Specifies the callback that is invoked when a tab close button is clicked.
|
||||||
* The callback is responsible for closing the tab.
|
* The callback is responsible for closing the tab.
|
||||||
|
|||||||
@@ -1533,6 +1533,8 @@ public class FlatTabbedPaneUI
|
|||||||
private final PropertyChangeListener contentListener = this::contentPropertyChange;
|
private final PropertyChangeListener contentListener = this::contentPropertyChange;
|
||||||
|
|
||||||
private int pressedTabIndex = -1;
|
private int pressedTabIndex = -1;
|
||||||
|
private int lastTipTabIndex = -1;
|
||||||
|
private String lastTip;
|
||||||
|
|
||||||
void installListeners() {
|
void installListeners() {
|
||||||
tabPane.addMouseMotionListener( this );
|
tabPane.addMouseMotionListener( this );
|
||||||
@@ -1565,7 +1567,7 @@ public class FlatTabbedPaneUI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed( MouseEvent e ) {
|
public void mousePressed( MouseEvent e ) {
|
||||||
updateRollover( e, true );
|
updateRollover( e );
|
||||||
|
|
||||||
if( !isPressedTabClose() )
|
if( !isPressedTabClose() )
|
||||||
mouseDelegate.mousePressed( e );
|
mouseDelegate.mousePressed( e );
|
||||||
@@ -1574,20 +1576,20 @@ public class FlatTabbedPaneUI
|
|||||||
@Override
|
@Override
|
||||||
public void mouseReleased( MouseEvent e ) {
|
public void mouseReleased( MouseEvent e ) {
|
||||||
if( isPressedTabClose() ) {
|
if( isPressedTabClose() ) {
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
if( pressedTabIndex >= 0 && pressedTabIndex == getRolloverTab() )
|
if( pressedTabIndex >= 0 && pressedTabIndex == getRolloverTab() )
|
||||||
closeTab( pressedTabIndex );
|
closeTab( pressedTabIndex );
|
||||||
} else
|
} else
|
||||||
mouseDelegate.mouseReleased( e );
|
mouseDelegate.mouseReleased( e );
|
||||||
|
|
||||||
pressedTabIndex = -1;
|
pressedTabIndex = -1;
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseEntered( MouseEvent e ) {
|
public void mouseEntered( MouseEvent e ) {
|
||||||
// this is necessary for "more tabs" button
|
// this is necessary for "more tabs" button
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1595,22 +1597,22 @@ public class FlatTabbedPaneUI
|
|||||||
// this event occurs also if mouse is moved to a custom tab component
|
// this event occurs also if mouse is moved to a custom tab component
|
||||||
// that handles mouse events (e.g. a close button)
|
// that handles mouse events (e.g. a close button)
|
||||||
// --> make sure that the tab stays highlighted
|
// --> make sure that the tab stays highlighted
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
//---- interface MouseMotionListener ----
|
//---- interface MouseMotionListener ----
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged( MouseEvent e ) {
|
public void mouseDragged( MouseEvent e ) {
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved( MouseEvent e ) {
|
public void mouseMoved( MouseEvent e ) {
|
||||||
updateRollover( e, false );
|
updateRollover( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateRollover( MouseEvent e, boolean pressed ) {
|
private void updateRollover( MouseEvent e ) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
int y = e.getY();
|
int y = e.getY();
|
||||||
|
|
||||||
@@ -1622,10 +1624,41 @@ public class FlatTabbedPaneUI
|
|||||||
boolean hitClose = isTabClosable( tabIndex )
|
boolean hitClose = isTabClosable( tabIndex )
|
||||||
? getTabCloseHitArea( tabIndex ).contains( x, y )
|
? getTabCloseHitArea( tabIndex ).contains( x, y )
|
||||||
: false;
|
: false;
|
||||||
if( pressed )
|
if( e.getID() == MouseEvent.MOUSE_PRESSED )
|
||||||
pressedTabIndex = hitClose ? tabIndex : -1;
|
pressedTabIndex = hitClose ? tabIndex : -1;
|
||||||
setRolloverTabClose( hitClose );
|
setRolloverTabClose( hitClose );
|
||||||
setPressedTabClose( hitClose && tabIndex == pressedTabIndex );
|
setPressedTabClose( hitClose && tabIndex == pressedTabIndex );
|
||||||
|
|
||||||
|
// update tooltip
|
||||||
|
if( tabIndex >= 0 && hitClose ) {
|
||||||
|
Object closeTip = getTabClientProperty( tabIndex, TABBED_PANE_TAB_CLOSE_TOOLTIPTEXT );
|
||||||
|
if( closeTip instanceof String )
|
||||||
|
setCloseToolTip( tabIndex, (String) closeTip );
|
||||||
|
else
|
||||||
|
restoreTabToolTip();
|
||||||
|
} else
|
||||||
|
restoreTabToolTip();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setCloseToolTip( int tabIndex, String closeTip ) {
|
||||||
|
if( tabIndex == lastTipTabIndex )
|
||||||
|
return; // closeTip already set
|
||||||
|
|
||||||
|
if( tabIndex != lastTipTabIndex )
|
||||||
|
restoreTabToolTip();
|
||||||
|
|
||||||
|
lastTipTabIndex = tabIndex;
|
||||||
|
lastTip = tabPane.getToolTipTextAt( lastTipTabIndex );
|
||||||
|
tabPane.setToolTipTextAt( lastTipTabIndex, closeTip );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreTabToolTip() {
|
||||||
|
if( lastTipTabIndex < 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tabPane.setToolTipTextAt( lastTipTabIndex, lastTip );
|
||||||
|
lastTip = null;
|
||||||
|
lastTipTabIndex = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---- interface PropertyChangeListener ----
|
//---- interface PropertyChangeListener ----
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ public class FlatContainerTest
|
|||||||
|
|
||||||
tabsClosableCheckBox.setSelected( true );
|
tabsClosableCheckBox.setSelected( true );
|
||||||
tabsClosableChanged();
|
tabsClosableChanged();
|
||||||
|
putTabbedPanesClientProperty( TABBED_PANE_TAB_CLOSE_TOOLTIPTEXT, "Close" );
|
||||||
|
|
||||||
tabScrollCheckBox.setSelected( true );
|
tabScrollCheckBox.setSelected( true );
|
||||||
tabScrollChanged();
|
tabScrollChanged();
|
||||||
@@ -129,14 +130,15 @@ public class FlatContainerTest
|
|||||||
|
|
||||||
private void addInitialTabs( JTabbedPane... tabbedPanes ) {
|
private void addInitialTabs( JTabbedPane... tabbedPanes ) {
|
||||||
for( JTabbedPane tabbedPane : tabbedPanes ) {
|
for( JTabbedPane tabbedPane : tabbedPanes ) {
|
||||||
tabbedPane.addTab( "Tab 1", new Panel1() );
|
tabbedPane.addTab( "Tab 1", null, new Panel1(), "First tab." );
|
||||||
|
|
||||||
JComponent tab2 = new Panel2();
|
JComponent tab2 = new Panel2();
|
||||||
tab2.setBorder( new LineBorder( Color.magenta ) );
|
tab2.setBorder( new LineBorder( Color.magenta ) );
|
||||||
tabbedPane.addTab( "Second Tab", tab2 );
|
tabbedPane.addTab( "Second Tab", null, tab2, "This is the second tab." );
|
||||||
|
|
||||||
addTab( tabbedPane, "Disabled", "tab content 3" );
|
addTab( tabbedPane, "Disabled", "tab content 3" );
|
||||||
tabbedPane.setEnabledAt( 2, false );
|
tabbedPane.setEnabledAt( 2, false );
|
||||||
|
tabbedPane.setToolTipTextAt( 2, "Disabled tab." );
|
||||||
|
|
||||||
tabbedPane.addTab( "Tab 4", new JLabel( "non-opaque content", SwingConstants.CENTER ) );
|
tabbedPane.addTab( "Tab 4", new JLabel( "non-opaque content", SwingConstants.CENTER ) );
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user