A
JMenuBar
is the interface to the Java Swing JMenuBar Component.
Yoix programs normally interact with them by adding event handlers
to the menu items in the menus within the menubar or to the frame or dialog
that contains the menubar, and by reading or writing the following
JMenuBar
fields:
| background |
The
Color
that is used to paint the background of the menubar.
It is also the default background color assigned to components in the
items
array that do not pick their own.
The default value is
NULL,
which means use the background color of the frame or dialog that owns
the menubar.
Reading returns a snapshot of the current color.
Writing immediately sets the menubar's background,
and the background of components contained in the menubar
that do not pick their own, to the new color.
| | border |
An
Object
that should be a
Border,
Insets,
Number,
or
String
that describes the border that is drawn around this menubar.
A
NULL
value, which is the default, means no border.
A
border
that is an
Insets
or
Number
is an easy way to describe margins
(i.e., an
EmptyBorder),
in units of 72 dots per inch, that are left around the sides of this menubar.
A
border
that is a
String
is a quick way to surround this menubar with a border that uses the
String
as its title.
Reading returns a snapshot of the current border.
Writing immediately sets the menubar's border to the new value.
| | components |
A
Dictionary
maintained by the interpreter that maps
tag
fields to actual menu components;
every menu component contained in the menubar has an entry in the dictionary.
| | cursor |
An
Object
that should be an
int,
Image,
or
String
that selects the cursor shown when the pointer is over the menubar.
It is also the default cursor assigned to components in the menubar's
layout
array that do not pick their own.
A
cursor
that is an
int
should be one of the cursors defined in the
yoix.awt.Cursor
dictionary.
A
cursor
that is an
Image
can describe the cursor using its
size
and
hotspot
fields and often draws it using its
paint
function.
A
cursor
that is a
String
should be the name of a cursor that is already defined in
yoix.awt.Cursor
or the name a local a file or URL that contains a GIF or JPEG image
that will be used as the cursor.
Reading returns the current cursor.
Writing immediately sets the menubar's cursor,
and the cursor of components contained in the menubar
that do not pick their own, to the new value.
A menubar that does not set its own cursor uses
DEFAULT_CURSOR.
| | doublebuffered |
An
int
that is
1
when the menubar uses double-buffering to draw itself,
0
when it does not, and starts with a default value that is
selected by Java for the menubar.
Reading returns the current double-buffering behavior.
Writing immediately sets the menubar's double-buffering behavior to the new value.
Note that double-buffering may be required when transparent components are used.
| | dragenabled |
An
int
that should be set to
1
when this menubar wants to use the automatic drag handling that Swing provides
for some components, and
0
(the default)
when it does not.
Components that do not provide automatic drag handling always return
0
when their
dragenabled
field is read, so storing
1
in
dragenabled
should only be viewed as a request for a service that may not be available.
Swing components can always take complete control of their drag and drop
handling using their
transferhandler
field or special drag and drop event handlers.
| | enabled |
An
int
that is
1
when the menubar can respond to user input, and
0
when it can not.
The top-level application window that contains the menubar always gets the
final say, so disabling that window always disables the menubar.
Reading returns the current state.
Writing immediately sets the menubar's state to the new value.
| | focusable |
An
int
that is non-zero (the default)
when the menubar can accept the keyboard focus and zero
when it can not.
Reading returns the menubar's current focusable state.
Writing immediately changes the menubar's focusable state to the new value,
which means the focus is automatically transferred if the new value is zero
and the menubar is the current focus owner.
| | focusowner |
A read-only
int
that is non-zero when the menubar has the focus.
| | font |
The
Font,
or font name if it is a
String,
that is used as the default font assigned to menubar and the components in the
items
array that do not pick their own.
The default value is
NULL,
which means use the font selected by the frame or dialog that owns the menubar.
Reading returns a snapshot of the current font.
Writing immediately changes the menubar's font,
and the font used by all components contained in the menubar
that do not pick their own, to the new font.
| | foreground |
The
Color
that is used as the default foreground color assigned to
the menubar and components in the
items
array that do not pick their own.
The default value is
NULL,
which means use the foreground color of the frame or dialog that owns
the menubar.
Reading returns a snapshot of the current color.
Writing immediately sets the menubar's foreground,
and the foreground of components contained in the menubar
that do not pick their own, to the new color.
| | getEnabled(Object pattern) |
A
Builtin
that finds the first menu item with a command that matches
pattern,
which can be a
String
or
Regexp,
and returns
1
if it is currently enabled (i.e., it will respond when selected by a user),
0
if not, and
-1
if there is no match.
| | getState(Object pattern) |
A
Builtin
that finds the first checkbox or radiobutton menu item with a command that matches
pattern,
which can be a
String
or
Regexp,
and returns
1
if it is currently selected,
0
if not, and
-1
if there is no match.
| | insets |
An
Object,
usually an
Insets
or
Number,
that describes the margins, in units of 72 dots per inch, that
separate the menubar from its default border.
This field is not automatically used when the menubar builds its own
border.
An
insets
that is a number is an easy way to describe a uniform margin.
Writing immediately changes the margins that are drawn around the menubar
by the default border.
| | items |
A
Menu
that describes the menubar configuration or an
Array
of the
JMenu
objects that are actually in the menubar.
When
items
is a
Menu
it must be organized in pairs.
The first element in each pair must be a string that is used as a label
on the menubar.
The second element in each pair must be another
Menu
array that describes the submenu that is placed under its label on the
menubar.
Writing causes the new
Array
or
Menu
to be parsed and if there are no errors immediately resets the menu's contents.
As a special case, an
int
value of
RIGHT
in the
Array
indicates that all following menu items should be aligned to the right of the menubar.
| | location |
A
Point
that determines the location of the menubar
in a coordinate system that has its origin at the upper left corner
of the container closest to the menubar (in the component hierarchy)
that actually contains it, positive x to the right, positive y down,
and a resolution of 72 dots per inch.
Reading returns a snapshot of the current location.
Writing is allowed, but layout managers usually get the final say, so setting
location
should be viewed as a request that may not be honored.
| | nextfocus |
An
Object
that identifies the component that receives the focus after this menubar
when the focus traverses from one component to the next
(usually by means of the keyboard
TAB
character).
When
nextfocus
is a
String
it is assumed to be the tag associated with the target component,
which must belong to the same top-level container (e.g., the same
JFrame)
as this menubar.
A
NULL
value means the component that gets the focus after this menubar will be
selected by Java's default focus traversal policy.
Reading returns the value last stored or
NULL
if the value was a
String
that did not reference a component.
Writing immediately changes the component that gets the focus after this menubar.
| | popup |
A
JPopupMenu
that is associated with the menubar.
Reading returns the current popup menu.
Writing immediately shows the popup menu at the point in the menubar's coordinate
system specified by the popup menu's
location
field, assuming of course that the menubar is showing on the screen.
Storing
TRUE
in the popup menu's
visible
field, which was added in release 1.2.0, is an easy way to show the
popup menu that currently belongs to the menubar.
| | preferredsize |
A
Dimension
that is used by layout managers when they need to know the menubar's
preferred size in units of 72 dots per inch.
A
NULL
value means the menubar has no preference.
A non-positive height or width is allowed and simply means the menubar
has no preference for that dimension.
Reading returns the current preferred size.
Writing changes the preferred size and immediately notifies
root.layoutmanager,
which means the components contained in
root
may be repositioned and resized.
| | root |
An
Object
that is automatically updated by the interpreter so it always references
the frame or dialog that currently owns the menubar.
A
NULL
value means the menubar is not attached to a frame or dialog.
| | setEnabled(Object pattern, int state) |
A
Builtin
that finds menu items with commands that match
pattern,
which can be a
String
or
Regexp,
and enables them (i.e., they respond when selected by a user) when
state
is non-zero and disables them when
state
is zero.
A return value of
-1
means there were no matches.
| | setState(Object pattern, int state) |
A
Builtin
that finds checkbox or radiobutton menu items with commands that match
pattern,
which can be a
String
or
Regexp,
and selects them when
state
is non-zero and deselects them when
state
is zero.
A return value of
-1
means there were no matches.
| | showing |
A read-only
int
that is non-zero when the menubar is showing on the screen.
| | size |
A
Dimension
that determines the size of the menubar
in units of 72 dots per inch.
Reading returns a snapshot of the current size.
Writing is allowed, but layout managers usually get the final say, so setting
size
should be viewed as a request that may not be honored.
| | tooltiptext |
A
String
of characters that is displayed in a tightly sized pop-up window that
appears near the cursor whenever the cursor lingers over the menubar.
Setting this value to
NULL
(the default)
disables the tooltip mechanism.
Reading returns the current tooltip text.
Writing immediately sets the new tooltip text.
| | transferhandler |
An
Object
that should be a
TransferHandler
or
String
that determines how the menubar handles data transfer operations
like drag and drop.
A value that is a
String
but not
""
means the field named by the
String
should be used as the source and sink of the data that is transferred by
the menubar.
The result is the same as assigning the
String
to the
property
field in a
TransferHandler
and then assigning that
TransferHandler
to
transferhandler.
The empty
String
""
is special and refers to the
TransferHandler
that Swing uses for automatic drag handling, if there is one.
Swing components that provide automatic drag handling start out with a
transferhandler
field that is not
NULL,
but the automatic drag handling is not enabled until
1
is stored in
dragenabled.
Swing components that provide their own drag and drop event handlers
currently must store
NULL
in
transferhandler
before those event handlers will start working.
| | visible |
An
int
that is
1
when the menubar is visible, and
0
otherwise.
Reading returns the current visibility.
Writing immediately sets the menubar's visibility to the new state.
|
Several permanent fields have not been documented and should not be
used in Yoix applications.
In addition, even though the
layer,
opaque,
nextfocus,
requestfocus,
and
requestfocusenabled
fields are defined, in most situations they have no effect and as a result
they have not been documented.
A menubar can be attached to a top-level container such as a frame or dialog
by assigning it to the container's
menubar
field.
Only one container can own a menubar, so attaching it to a container
automatically removes the menubar from the current owner,
if there is one.
Storing
NULL
in a container's
menubar
field removes the current menubar.
Selecting a regular menu item from the menubar delivers an
ActionEvent
to the
actionPerformed
event handler defined either in the item itself or
in the container that currently owns the menubar.
Selecting a checkbox menu item delivers an
ItemEvent
to the
itemStateChanged
event handler defined in that same container or in the item itself.
With respect to the
JMenuBar
itself, the
actionPerformed
and
itemStateChanged
event handlers are only for popup menus.
| |
| Event Handlers: |
actionPerformed,
componentHidden,
componentMoved,
componentResized,
componentShown,
dragDropEnd,
dragEnter,
dragExit,
dragGestureRecognized,
dragMouseMoved,
dragOver,
drop,
dropActionChanged,
focusGained,
focusLost,
invocationRun,
itemStateChanged,
keyPressed,
keyReleased,
keyTyped,
mouseClicked,
mouseDragged,
mouseEntered,
mouseExited,
mouseMoved,
mousePressed,
mouseReleased,
mouseWheelMoved
| | |
| Example: |
The program,
import yoix.*.*;
JMenuBar menubar = {
Menu items = {
"File", new Array {
"Open", new Dictionary {
String command = "open";
String accelerator = "ctrl O";
int enabled = FALSE;
},
"Close", new Dictionary {
String command = "close";
String accelerator = "ESCAPE";
},
"-", NULL,
"Exit", "exit",
},
"Edit", new Array {
"Save", "save1", 1,
"Save As", "save2",
},
};
};
JFrame f = {
JMenuBar menubar = menubar;
GridLayout layoutmanager = {
int rows = 1;
};
Array layout = {
new JButton {
String text = "Toggle MenuBar";
actionPerformed(e) {
if (root.menubar == NULL)
root.menubar = global.menubar;
else root.menubar = NULL;
}
},
new JButton {
String text = "Show MenuBar";
actionPerformed(e) {
root.menubar = global.menubar;
}
},
new JButton {
String text = "Hide MenuBar";
actionPerformed(e) {
root.menubar = NULL;
}
},
};
actionPerformed(e) {
printf("actionPerformed Received: %O\n", e);
if (strcmp(e.command, "exit") == 0)
exit(0);
}
itemStateChanged(e) {
printf("itemStateChanged Received: %O\n", e);
}
};
f.visible = TRUE;
adds and removes a menubar from a frame and shows how menu selections
get back to the frame's
actionPerformed
and
itemStateChanged
event handlers.
| | |
| See Also: |
BevelBorder,
Border,
EmptyBorder,
EtchedBorder,
invokeLater,
JButton,
JCanvas,
JCheckBox,
JCheckBoxMenuItem,
JChoice,
JColorChooser,
JComboBox,
JDesktopPane,
JDialog,
JFileChooser,
JFileDialog,
JFrame,
JInternalFrame,
JLabel,
JLayeredPane,
JList,
JMenu,
JMenuItem,
JPanel,
JPasswordField,
JPopupMenu,
JProgressBar,
JRadioButton,
JRadioButtonMenuItem,
JScrollBar,
JScrollPane,
JSeparator,
JSlider,
JSplitPane,
JTabbedPane,
JTable,
JTextArea,
JTextCanvas,
JTextField,
JTextPane,
JTextTerm,
JToggleButton,
JToolBar,
JTree,
JWindow,
LineBorder,
MatteBorder,
Menu,
postEvent,
SoftBevelBorder,
TransferHandler
|
|