Professional Documents
Culture Documents
The Javax - Swing Package Contains Several Classes Used For Creating Menus
The Javax - Swing Package Contains Several Classes Used For Creating Menus
The Javax - Swing Package Contains Several Classes Used For Creating Menus
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
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.
Therefore, it is
created using a
nested loop.
We add the
JScrollPane to the
frame instead of
directly adding the
JTable to the frame.
Out put