The Javax - Swing Package Contains Several Classes Used For Creating Menus

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

The javax.

swing package contains


several classes used for creating Menus
 JMenuBar, JMenu, JMenuItem (and variants):
 JRadioButtonMenuItem
 JCheckboxMenuItem

 Only the JMenuItem objects (and variants)


generate events.

Edit View Help


JMenuItems generate
Action Events
Some Events and Their Associated Event Listeners
Act that Results in the Event Event Listener Type
Action: User clicks a button, presses Enter while
ActionListener
typing in a text field, or chooses a menu item
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is
MouseListener
over a component
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as
PropertyChangeListener
the text on a label
JMenuBar
 JMenuBar is a bar where the menus are
placed.
There is only one menu bar per frame.

File Edit View Help


JMenuBar
JMenu
 JMenu (such as File or Edit) is a group of
menu choices.
A JMenuBar object may include many JMenu
objects.

JMenu
JMenuItem
 JMenuItem (such as Copy, Cut, or Paste) is an
individual menu choice in a JMenu object.
 Action command defaults to text of menu item
Override with call to setActionCommand()
Useful when providing UI in multiple languages

JMenuItem

JSeparator
Demo on Menus
Sequence for Creating Menus
1. Create a JMenuBar object and add it to a
frame via setJMenuBar()
2. Create a JMenu object.
3. Create JMenuItem objects and add them
to the JMenu object.
4. Add the JMenu object to the JMenuBar
object.
Radio buttons & Check boxes
 Along with JButton, Radio buttons and
Check boxes are all specific kinds of
buttons – they all derive from the
AbstractButton class
This is called inheritance – coming soon
 This implies similar behavior for these
classes
Check boxes
 Implemented by JCheckBox
 A GUI usually permits more
than one Check box to be
selected at one time
Radio Buttons
 Implemented by
JRadioButton
 A GUI usually prevents
more than one Radio
button to be selected at
once
 Typically used along with a
ButtonGroup object
 ButtonGroup takes care of
deselecting the previous
Radio button when a new
one is selected
Mouse Events
 Mouse events arise from such
user interactions as
 moving the mouse
 dragging the mouse (moving the
mouse while the mouse button is
being pressed)
 clicking the mouse buttons.
 Who listens for Mouse Events?
 JFrame
 ContentPane
 … or whoever is interested in getting
events
Handling Mouse Events
 A MouseListener handles most mouse events:
 mouseEntered – cursor crossed into the boundary
 mouseExited – cursor crossed out of the boundary
 mousePressed – button down (specifics in MouseEvent)
 mouseReleased – button up
 mouseClicked – button down/up in quick succession
 A MouseMotionListener handles mouse movement
 mouseDragged – mouse moved while button down
 mouseMoved – mouse moved while button up
○ Point location info in MouseEvent
○ Location is relative to the listening components origin
JTabbedPane
 A component that lets the user switch between
a group of components by clicking on a tab with
a given title and/or icon
 Constructors
JTabbedPane()
 Adding Tabs:
addTab(tabname,component)
○ Places the specified component into the tabl and gives it the specified
name.
Tab 1 has the label

Tab 2 has the panel


containing a label
and a text field
JScrollPane
 Provides a scrollable view of any
lightweight Swing component

 Constructor
JScrollPane(anyComponent)
The Vector Class
 in the java.util package
 resizable array containing references to Object instances
(or instances of Object subclasses)
Can be indexed
Can have elements added and deleted
 Some useful methods:
elementAt(int) – returns the object referenced by specified
element
addElement(Object) – adds the object to the end of the Vector
removeElementAt(int) – deletes the element at the specified index
removeAllElements() – empties out the vector
Simple example
using JTable,
Vector, JScrollPane
A JTable will
require two
collections (vectors
or arrays).

There will be a 2D
vector or array
containing the data
displayed in the
table.

There will also be a


1D array or vector
containing the
column headers.
Here we set up the
column-header
vector by adding
strings to it.

There will be a total


of four columns in
this example.
Here we set up the
data vector for the
JTable.

Note that this is a


vector of vectors
(that is, a 2-
dimensional
vector).

Therefore, it is
created using a
nested loop.

Outer loop sets up


the rows, inner loop
sets up the specific
elements within a
row.
In order to ensure
that the JTable is
scrollable, we put it
into a JScrollPane.

We add the
JScrollPane to the
frame instead of
directly adding the
JTable to the frame.
Out put

You might also like