org.xnap.commons.gui
Class CloseableTabbedPane

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JTabbedPane
                  extended by org.xnap.commons.gui.CloseableTabbedPane
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants

public class CloseableTabbedPane
extends JTabbedPane

Provides a JTabbedPane with close buttons in the tab titles. The buttons of the currently active tab will be visible and the button of the tab the mouse if hovered over.

If a button is clicked the tab is removed from the pane unless a CloseListener has been set.

Note: If a button is displayed on a non active tab and the mouse is moved very fast out of the tabbed pane, the button may still be visible.

Author:
Felix Berger, Steffen Pingel
See Also:
Serialized Form

Nested Class Summary
static interface CloseableTabbedPane.CloseListener
           
static interface CloseableTabbedPane.TabTitle
           
protected  class CloseableTabbedPane.TabTitleComponent
           
protected  class CloseableTabbedPane.TabTitleIcon
          Provides an Icon that can displays a text that can have an icon to its left and an EventIcon to its right.
 
Nested classes/interfaces inherited from class javax.swing.JTabbedPane
JTabbedPane.AccessibleJTabbedPane, JTabbedPane.ModelListener
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JTabbedPane
changeEvent, changeListener, model, SCROLL_TAB_LAYOUT, tabPlacement, WRAP_TAB_LAYOUT
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
CloseableTabbedPane()
          Creates a tabbed pane with close buttons and a WRAP_TAB_LAYOUT
 
Method Summary
 void addTab(String title, Component component)
          Convenience wrapper for addTab(String, Component, null, true).
 void addTab(String title, Component component, Icon icon)
          Convenience wrapper for addTab(String, Component, Icon, true).
 void addTab(String title, Component component, Icon icon, boolean closeable)
          Adds a compnent with a tab title and an icon to the tabbed pane.
 String getTitleAt(int index)
          This method does not work properly for tabs that have a close button.
 void removeTabAt(int index)
           
 void setCloseListener(CloseableTabbedPane.CloseListener listener)
          Sets the listener that is notified of user initiated close requests for single tabs.
 void setTabLayoutPolicy(int tabLayoutPolicy)
          Sets the tab layout policy.
 void setTitleAt(int index, String newTitle)
           
 void updateUI()
           
 
Methods inherited from class javax.swing.JTabbedPane
add, add, add, add, add, addChangeListener, addTab, addTab, createChangeListener, fireStateChanged, getAccessibleContext, getBackgroundAt, getBoundsAt, getChangeListeners, getComponentAt, getDisabledIconAt, getDisplayedMnemonicIndexAt, getForegroundAt, getIconAt, getMnemonicAt, getModel, getSelectedComponent, getSelectedIndex, getTabCount, getTabLayoutPolicy, getTabPlacement, getTabRunCount, getToolTipText, getToolTipTextAt, getUI, getUIClassID, indexAtLocation, indexOfComponent, indexOfTab, indexOfTab, insertTab, isEnabledAt, paramString, remove, remove, removeAll, removeChangeListener, setBackgroundAt, setComponentAt, setDisabledIconAt, setDisplayedMnemonicIndexAt, setEnabledAt, setForegroundAt, setIconAt, setMnemonicAt, setModel, setSelectedComponent, setSelectedIndex, setTabPlacement, setToolTipTextAt, setUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CloseableTabbedPane

public CloseableTabbedPane()
Creates a tabbed pane with close buttons and a WRAP_TAB_LAYOUT

Method Detail

setCloseListener

public void setCloseListener(CloseableTabbedPane.CloseListener listener)
Sets the listener that is notified of user initiated close requests for single tabs.

Parameters:
listener - can be null, then the component is simply removed

addTab

public void addTab(String title,
                   Component component,
                   Icon icon,
                   boolean closeable)
Adds a compnent with a tab title and an icon to the tabbed pane.

The icon is displayed on the left side of the title and does not affect the icon of the closing button for this tab.

Parameters:
title - the title of the tab, can be null
component -
icon - can be null
closeable - whether or not a close button should be displayed for this tabbed pane.

addTab

public void addTab(String title,
                   Component component,
                   Icon icon)
Convenience wrapper for addTab(String, Component, Icon, true).


addTab

public void addTab(String title,
                   Component component)
Convenience wrapper for addTab(String, Component, null, true).

Overrides:
addTab in class JTabbedPane

setTitleAt

public void setTitleAt(int index,
                       String newTitle)
Overrides:
setTitleAt in class JTabbedPane

getTitleAt

public String getTitleAt(int index)
This method does not work properly for tabs that have a close button.

Keep that information somewhere else if you need it.

Overrides:
getTitleAt in class JTabbedPane
Returns:
the empty string for tabs that have a close button, the title of the tab otherwise

setTabLayoutPolicy

public void setTabLayoutPolicy(int tabLayoutPolicy)
Sets the tab layout policy. Currently WRAP_TAB_LAYOUT is strongly recommended.

Explanation: SCROLL_TAB_LAYOUT uses a private view translation for the scrolling of the tabs in BasicTabbedPaneUI. The EventIcon.paint() method receives coordinates from the ui view port and since it is not possible to translate coordinates between the tabbed pane and the view port the closing button can not be placed at the correct location.

However there is a crude work around: TabbedPaneUI.getTabBounds() returns tabbed pane coordinates whereas TabTitleIcon.paint() receives view port coordinates. When TabTitleIcon.paint() is invoked it can finds out its tab bounds in tabbed pane coordinates and use those to set the button's bounds. The location can only be somewhat estimated as it depends on the tab style of the look&feel.

Overrides:
setTabLayoutPolicy in class JTabbedPane
Parameters:
tabLayoutPolicy - must be JTabbedPane.WRAP_TAB_LAYOUT
See Also:
JTabbedPane.setTabLayoutPolicy(int)

updateUI

public void updateUI()
Overrides:
updateUI in class JTabbedPane

removeTabAt

public void removeTabAt(int index)
Overrides:
removeTabAt in class JTabbedPane


Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.