Week2A 03april2024 JMenu and ActionEvents
Week2A 03april2024 JMenu and ActionEvents
Radford Burger
Agenda
GUI: Re-cap
LayoutManagers
Menus
Inner class for event-handling
Gui: Recap
Every user interface considers the
following three main aspects −
Every SWING controls inherits properties from the following Component class hiearchy.
Gui: Recap
list of commonly used controls while designing GUI using SWING:
Class Description
JLabel
Class Placing text in a container
Description
JButton
JLabel
JCheckBox
JButton
JRadioButton
JCheckBox
JColorChooser
JRadioButton
JTextField
JColorChooser Editing of single line of text
Class description
BorderLayout Arranges components to fit in 5 regions
GridLayout Places components in a rectangular grid
FlowLayout Arranges components in a directional flow
CardLayout Treats each component as a card. Only 1
card is visible at a time
GridBagLayout Aligns components vertically or
horizontally, or along their baseline
Gui: Menus
As we know that every top-level window has a menu
bar associated with it.
This menu bar consists of various menu choices
available to the end user.
Further, each choice contains a list of items or options,
which is called menu items.
Gui: Menus
list of menu controls:
Class description
JMenuBar Top-level window
JMenu Pull-down menu component displayed
from the menubar
JMenuItem Available options in the pull-down menu
Gui: Menus
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
2. Create JMenu object
3. Create JMenuItem objects and attach to JMenu
object
4. Attach JMenu object to JMenuBar object
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
mBar.add(fileMenu);
Sequence of steps to create
menus
4. Create JMenuItem objects and attach to JMenu
object
JMenuItem newItem = new JMenuItem(“New record”);
JMenuItem updateItem = new JMenuItem(“Update
record”);
fileMenu.add(newItem);
fileMenu.add(updateItem);
Event-handling of menus
remove
Event
han
anonym dling by
ous cla
ss
replace
This method
will move
Alternative syntax for Event-handling
Alternative syntax for Event-handling