Java Reference
In-Depth Information
items; the remaining methods, such as
add(Component)
, are inherited from
Container
. In addition,
you can add a separator to a toolbar.
public JButton add(Action action);
public void addSeparator();
public void addSeparator(Dimension size);
When using the
add(Action)
method of
JToolBar
, the added
Action
is encapsulated within
a
JButton
object. This is different from adding actions to
JMenu
or
JPopupMenu
components, in
which
JMenuItem
objects are added instead. As with
JMenu
and
JPopupMenu
, adding an
Action
in
this fashion is discouraged in the Javadoc for the class. For separators, if you don't specify the
size, the installed look and feel forces a default size setting.
■
Note
For more information about dealing with the
Action
interface, see Chapter 2 or the section “Using
Action Objects with Menus” earlier in this chapter.
To remove components from a toolbar, use the following method:
public void remove(Component component)
JToolBar Properties
The
JToolBar
class defines nine properties, which are listed in Table 6-15.
Table 6-15.
JToolBar Properties
Property Name
Data Type
Access
accessibleContext
AccessibleContext
Read-only
borderPainted
boolean
Read-write bound
floatable
boolean
Read-write bound
layout
LayoutManager
Write-only
margin
Insets
Read-write bound
orientation
int
Read-write bound
rollover
boolean
Read-write bound
UI
ToolBarUI
Read-write
UIClassID
String
Read-only
By default, the border of a
JToolBar
is painted. If you don't want the border painted, you
can set the
borderPainted
property to
false
. Without using the
borderPainted
property,
you would need to change the setting of the
border
property (inherited from the superclass
JComponent
).