FileDocCategorySizeDatePackage
JToolBar.javaAPI DocJava SE 5 API27080Fri Aug 26 14:57:58 BST 2005javax.swing

JToolBar

public class JToolBar extends JComponent implements SwingConstants, Accessible
JToolBar provides a component that is useful for displaying commonly used Actions or controls. For examples and information on using tool bars see How to Use Tool Bars, a section in The Java Tutorial.

With most look and feels, the user can drag out a tool bar into a separate window (unless the floatable property is set to false). For drag-out to work correctly, it is recommended that you add JToolBar instances to one of the four "sides" of a container whose layout manager is a BorderLayout, and do not add children to any of the other four "sides".

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

beaninfo
attribute: isContainer true description: A component which displays commonly used controls or Actions.
version
1.108 02/26/04
author
Georges Saab
author
Jeff Shapiro
see
Action

Fields Summary
private static final String
uiClassID
private boolean
paintBorder
private Insets
margin
private boolean
floatable
private int
orientation
Constructors Summary
public JToolBar()
Creates a new tool bar; orientation defaults to HORIZONTAL.


                  
     
    
        this( HORIZONTAL );
    
public JToolBar(int orientation)
Creates a new tool bar with the specified orientation. The orientation must be either HORIZONTAL or VERTICAL.

param
orientation the orientation desired

	this(null, orientation);
    
public JToolBar(String name)
Creates a new tool bar with the specified name. The name is used as the title of the undocked tool bar. The default orientation is HORIZONTAL.

param
name the name of the tool bar
since
1.3

	this(name, HORIZONTAL);
    
public JToolBar(String name, int orientation)
Creates a new tool bar with a specified name and orientation. All other constructors call this constructor. If orientation is an invalid value, an exception will be thrown.

param
name the name of the tool bar
param
orientation the initial orientation -- it must be either HORIZONTAL or VERTICAL
exception
IllegalArgumentException if orientation is neither HORIZONTAL nor VERTICAL
since
1.3

	setName(name);
        checkOrientation( orientation );

	this.orientation = orientation;
	DefaultToolBarLayout layout =  new DefaultToolBarLayout( orientation );
	setLayout( layout );

        addPropertyChangeListener( layout );

        updateUI();
    
Methods Summary
public javax.swing.JButtonadd(javax.swing.Action a)
Adds a new JButton which dispatches the action.

As of 1.3, this is no longer the preferred method for adding Actions to a container. Instead it is recommended to configure a control with an action using using setAction, and then add that control directly to the Container.

param
a the Action object to add as a new menu item
return
the new button which dispatches the action

	JButton b = createActionComponent(a);
	b.setAction(a);
        add(b);
        return b;
    
protected voidaddImpl(java.awt.Component comp, java.lang.Object constraints, int index)
If a JButton is being added, it is initially set to be disabled.

param
comp the component to be enhanced
param
constraints the constraints to be enforced on the component
param
index the index of the component

        if (comp instanceof Separator) {
            if (getOrientation() == VERTICAL) {
                ( (Separator)comp ).setOrientation(JSeparator.HORIZONTAL);
            } else {
                ( (Separator)comp ).setOrientation(JSeparator.VERTICAL);
            }
        }
        super.addImpl(comp, constraints, index);
        if (comp instanceof JButton) {
            ((JButton)comp).setDefaultCapable(false);
        }
    
public voidaddSeparator()
Appends a separator of default size to the end of the tool bar. The default size is determined by the current look and feel.

	addSeparator(null);
    
public voidaddSeparator(java.awt.Dimension size)
Appends a separator of a specified size to the end of the tool bar.

param
size the Dimension of the separator

        JToolBar.Separator s = new JToolBar.Separator( size );
        add(s);
    
private voidcheckOrientation(int orientation)

        switch ( orientation )
	{
            case VERTICAL:
            case HORIZONTAL:
                break;
            default:
                throw new IllegalArgumentException( "orientation must be one of: VERTICAL, HORIZONTAL" );
        }
    
protected java.beans.PropertyChangeListenercreateActionChangeListener(javax.swing.JButton b)
Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur, or null if the default property change listener for the control is desired.

As of 1.3, this is no longer the preferred method for adding Actions to a Container. Instead it is recommended to configure a control with an action using setAction, and then add that control directly to the Container.

return
null

	return null;
    
protected javax.swing.JButtoncreateActionComponent(javax.swing.Action a)
Factory method which creates the JButton for Actions added to the JToolBar. The default name is empty if a null action is passed.

As of 1.3, this is no longer the preferred method for adding Actions to a Container. Instead it is recommended to configure a control with an action using setAction, and then add that control directly to the Container.

param
a the Action for the button to be added
return
the newly created button
see
Action

	String text = a!=null? (String)a.getValue(Action.NAME) : null;
	Icon icon   = a!=null? (Icon)a.getValue(Action.SMALL_ICON) : null;
        boolean enabled = a!=null? a.isEnabled() : true;
        String tooltip = a!=null?
            (String)a.getValue(Action.SHORT_DESCRIPTION) : null;
        JButton b = new JButton(text, icon) {
	    protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
		PropertyChangeListener pcl = createActionChangeListener(this);
		if (pcl==null) {
		    pcl = super.createActionPropertyChangeListener(a);
		}
		return pcl;
	    }
	};
	if (icon !=null) {
	    b.putClientProperty("hideActionText", Boolean.TRUE);
	}
	b.setHorizontalTextPosition(JButton.CENTER);
	b.setVerticalTextPosition(JButton.BOTTOM);
	b.setEnabled(enabled);
	b.setToolTipText(tooltip);
	return b;
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JToolBar. For tool bars, the AccessibleContext takes the form of an AccessibleJToolBar. A new AccessibleJToolBar instance is created if necessary.

return
an AccessibleJToolBar that serves as the AccessibleContext of this JToolBar

        if (accessibleContext == null) {
            accessibleContext = new AccessibleJToolBar();
        }
        return accessibleContext;
    
public java.awt.ComponentgetComponentAtIndex(int i)
Returns the component at the specified index.

param
i the component's position, where 0 is first
return
the Component at that position, or null for an invalid index

        int ncomponents = this.getComponentCount();
        if ( i >= 0 && i < ncomponents) {
            Component[] component = this.getComponents();
            return component[i];
        }
        return null;
    
public intgetComponentIndex(java.awt.Component c)
Returns the index of the specified component. (Note: Separators occupy index positions.)

param
c the Component to find
return
an integer indicating the component's position, where 0 is first

        int ncomponents = this.getComponentCount();
        Component[] component = this.getComponents();
        for (int i = 0 ; i < ncomponents ; i++) {
            Component comp = component[i];
            if (comp == c)
                return i;
        }
        return -1;
    
public java.awt.InsetsgetMargin()
Returns the margin between the tool bar's border and its buttons.

return
an Insets object containing the margin values
see
Insets

         if(margin == null) {
             return new Insets(0,0,0,0);
         } else {
             return margin;
         }
     
public intgetOrientation()
Returns the current orientation of the tool bar. The value is either HORIZONTAL or VERTICAL.

return
an integer representing the current orientation -- either HORIZONTAL or VERTICAL
see
#setOrientation

        return this.orientation;
    
public javax.swing.plaf.ToolBarUIgetUI()
Returns the tool bar's current UI.

see
#setUI

        return (ToolBarUI)ui;
    
public java.lang.StringgetUIClassID()
Returns the name of the L&F class that renders this component.

return
the string "ToolBarUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

        return uiClassID;
    
public booleanisBorderPainted()
Gets the borderPainted property.

return
the value of the borderPainted property
see
#setBorderPainted

         return paintBorder;
     
public booleanisFloatable()
Gets the floatable property.

return
the value of the floatable property
see
#setFloatable

        return floatable;
    
public booleanisRollover()
Returns the rollover state.

return
true if rollover toolbar buttons are to be drawn; otherwise false
see
#setRollover(boolean)
since
1.4

	Boolean rollover = (Boolean)getClientProperty("JToolBar.isRollover");
	if (rollover != null) {
	    return rollover.booleanValue();
	}
	return false;
    
protected voidpaintBorder(java.awt.Graphics g)
Paints the tool bar's border if the borderPainted property is true.

param
g the Graphics context in which the painting is done
see
JComponent#paint
see
JComponent#setBorder

         if (isBorderPainted())
	 {
             super.paintBorder(g);
         }
     
protected java.lang.StringparamString()
Returns a string representation of this JToolBar. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

return
a string representation of this JToolBar.

        String paintBorderString = (paintBorder ?
				    "true" : "false");
        String marginString = (margin != null ?
			       margin.toString() : "");
        String floatableString = (floatable ?
				  "true" : "false");
        String orientationString = (orientation == HORIZONTAL ?
                                    "HORIZONTAL" : "VERTICAL");

        return super.paramString() +
        ",floatable=" + floatableString +
        ",margin=" + marginString +
        ",orientation=" + orientationString +
        ",paintBorder=" + paintBorderString;
    
public voidsetBorderPainted(boolean b)
Sets the borderPainted property, which is true if the border should be painted. The default value for this property is true. Some look and feels might not implement painted borders; they will ignore this property.

param
b if true, the border is painted
see
#isBorderPainted
beaninfo
description: Does the tool bar paint its borders? bound: true expert: true

         if ( paintBorder != b )
         {
	     boolean old = paintBorder;
	     paintBorder = b;
	     firePropertyChange("borderPainted", old, b);
	     revalidate();
	     repaint();
	 }
     
public voidsetFloatable(boolean b)
Sets the floatable property, which must be true for the user to move the tool bar. Typically, a floatable tool bar can be dragged into a different position within the same container or out into its own window. The default value of this property is true. Some look and feels might not implement floatable tool bars; they will ignore this property.

param
b if true, the tool bar can be moved; false otherwise
see
#isFloatable
beaninfo
description: Can the tool bar be made to float by the user? bound: true preferred: true

        if ( floatable != b )
	{
            boolean old = floatable;
	    floatable = b;

	    firePropertyChange("floatable", old, b);
	    revalidate();
	    repaint();
        }
    
public voidsetLayout(java.awt.LayoutManager mgr)

	LayoutManager oldMgr = getLayout();
	if (oldMgr instanceof PropertyChangeListener) {
	    removePropertyChangeListener((PropertyChangeListener)oldMgr);
	}
	super.setLayout(mgr);
    
public voidsetMargin(java.awt.Insets m)
Sets the margin between the tool bar's border and its buttons. Setting to null causes the tool bar to use the default margins. The tool bar's default Border object uses this value to create the proper margin. However, if a non-default border is set on the tool bar, it is that Border object's responsibility to create the appropriate margin space (otherwise this property will effectively be ignored).

param
m an Insets object that defines the space between the border and the buttons
see
Insets
beaninfo
description: The margin between the tool bar's border and contents bound: true expert: true

         Insets old = margin;
         margin = m;
         firePropertyChange("margin", old, m);
	 revalidate();
	 repaint();
     
public voidsetOrientation(int o)
Sets the orientation of the tool bar. The orientation must have either the value HORIZONTAL or VERTICAL. If orientation is an invalid value, an exception will be thrown.

param
o the new orientation -- either HORIZONTAL or VERTICAL
exception
IllegalArgumentException if orientation is neither HORIZONTAL nor VERTICAL
see
#getOrientation
beaninfo
description: The current orientation of the tool bar bound: true preferred: true

        checkOrientation( o );

	if ( orientation != o )
	{
	    int old = orientation;
	    orientation = o;

	    firePropertyChange("orientation", old, o);
	    revalidate();
	    repaint();
	}
    
public voidsetRollover(boolean rollover)
Sets the rollover state of this toolbar. If the rollover state is true then the border of the toolbar buttons will be drawn only when the mouse pointer hovers over them. The default value of this property is false.

The implementation of a look and feel may choose to ignore this property.

param
rollover true for rollover toolbar buttons; otherwise false
since
1.4
beaninfo
bound: true preferred: true attribute: visualUpdate true description: Will draw rollover button borders in the toolbar.

	putClientProperty("JToolBar.isRollover",
			  rollover ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetUI(javax.swing.plaf.ToolBarUI ui)
Sets the L&F object that renders this component.

param
ui the ToolBarUI L&F object
see
UIDefaults#getUI
beaninfo
bound: true hidden: true attribute: visualUpdate true description: The UI object that implements the Component's LookAndFeel.

        super.setUI(ui);
    
public voidupdateUI()
Notification from the UIFactory that the L&F has changed. Called to replace the UI with the latest version from the UIFactory.

see
JComponent#updateUI

        setUI((ToolBarUI)UIManager.getUI(this));
        // GTKLookAndFeel installs a different LayoutManager, and sets it
        // to null after changing the look and feel, so, install the default
        // if the LayoutManager is null.
        if (getLayout() == null) {
            setLayout(new DefaultToolBarLayout(getOrientation()));
        }
        invalidate();
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject and writeObject in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }