Java Reference
In-Depth Information
Action action = new ShowAction(frame);
JButton button = new JButton(action);
toolbar.add(button);
Container contentPane = frame.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(350, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
JToolBar.Separator Class
The
JToolBar
class maintains its own separator to permit a custom look and feel for the
separator when on a
JToolBar
.
This separator is automatically created when you call the
addSeparator()
method of
JToolBar
. In addition, there are two constructors for creating a
JToolBar.Separator
if you
want to manually create the component.
public JToolBar.Separator()
JSeparator toolBarSeparator = new JToolBar.Separator();
public JToolBar.Separator(Dimension size)
Dimension dimension = new Dimension(10, 10);
JSeparator toolBarSeparator = new JToolBar.Separator(dimension);
Both constructors create a horizontal separator. You can configure the size. If you don't
specify this, the look and feel decides what size to make the separator.
As with
JPopupMenu.Separator
, if you want to change the orientation of the separator, you
must call the
setOrientation()
method inherited from
JSeparator
, this time with an argument
of
JToolBar.Separator.VERTICAL
.
Summary
This chapter introduced the many Swing menu-related classes and their interrelationships,
and Swing's toolbar class. First, you learned about the
JMenuBar
and its selection model, and
learned how menu bars can be used within applets as well as applications.
Next, you explored the
JMenuItem
, which is the menu element the user selects, along with two
new event/listener pairs the system uses for dealing with events,
MenuKeyEvent
/
MenuKeyListener
and
MenuDragMouseEvent
/
MenuDragMouseListener
. Then, you moved on to the
JMenu
compo-
nent, upon which
JMenuItem
instances are placed, along with its new event/listener pair,
MenuEvent
/
MenuListener
, which is used to determine when a menu is about to be posted.