java day23 swings
TRANSCRIPT
-
8/6/2019 Java Day23 Swings
1/23
The javax.swing package contains
several classes used for creating Menus JMenuBar, JMenu, JMenuItem (and variants):
y JRadioButtonMenuItem
y JCheckboxMenuItem
Only the JMenuItem objects (and variants)generate events.
Edit View Help
-
8/6/2019 Java Day23 Swings
2/23
JMenuItems generate
Action EventsSome Events and Their Associated Event Listeners
Act that Results in the Event Event Listener Type
Action: User clicks a button, presses Enter while
typing in a text field, or chooses a menu itemActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is
over a componentMouseListener
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
the text on a labelPropertyChangeListener
-
8/6/2019 Java Day23 Swings
3/23
JMenuBar
JMenuBar is a bar where the menus are
placed.
y There is only one menu bar per frame.
JMenuBar
Edit View HelpFile
-
8/6/2019 Java Day23 Swings
4/23
JMenu
JMenu (such as File or Edit) is a group of
menu choices.
y A JMenuBar object may include many JMenu
objects.
JMenu
-
8/6/2019 Java Day23 Swings
5/23
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
y Override with call to setActionCommand()
y
Useful when providing UI in multiple languages
JMenuItem
JSeparator
-
8/6/2019 Java Day23 Swings
6/23
Demo on Menus
-
8/6/2019 Java Day23 Swings
7/23
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.
-
8/6/2019 Java Day23 Swings
8/23
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
y This is called inheritance coming soon
This implies similar behavior for these
classes
-
8/6/2019 Java Day23 Swings
9/23
-
8/6/2019 Java Day23 Swings
10/23
Radio Buttons
Implemented byJRadioButton
A GUI usually prevents
more than one Radiobutton to be selected atonce
y Typically used along with aButtonGroup object
y ButtonGroup takes care ofdeselecting the previousRadio button when a newone is selected
-
8/6/2019 Java Day23 Swings
11/23
Mouse Events
Mouse events arise from suchuser interactions as
y moving the mouse
y dragging the mouse (moving the
mouse while the mouse button is
being pressed)
y clicking the mouse buttons.
Who listens for Mouse Events?y JFrame
y
ContentPaney or whoever is interested in getting
events
-
8/6/2019 Java Day23 Swings
12/23
Handling Mouse Events
A MouseListener handles most mouse events:y mouseEntered cursor crossed into the boundary
y mouseExited cursor crossed out of the boundary
y mousePressed button down (specifics in MouseEvent)
y mouseReleased button up
y mouseClicked button down/up in quick succession
A MouseMotionListener handles mouse movement
y mouseDragged mouse moved while button down
y mouseMoved mouse moved while button up
Point location info in MouseEvent Location is relative to the listening components origin
-
8/6/2019 Java Day23 Swings
13/23
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
y JTabbedPane()
Adding Tabs:
y addTab(tabname,component)
Places the specifiedcomponentinto the tablandgives it the specified
name.
-
8/6/2019 Java Day23 Swings
14/23
-
8/6/2019 Java Day23 Swings
15/23
Tab 1 has the label
Tab 2 has the panelcontaining a label
and a text field
-
8/6/2019 Java Day23 Swings
16/23
JScrollPane
Provides a scrollable view of any
lightweight Swing component
Constructor
y JScrollPane(anyComponent)
-
8/6/2019 Java Day23 Swings
17/23
The Vector Class
in the java.util package resizable array containing references to Object
instances (or instances of Object subclasses)y Can be indexed
y Can have elements added and deleted
Some useful methods:y elementAt(int) returns the object referenced by specified
element
y addElement(Object) adds the object to the end of the Vector
y removeElementAt(int) deletes the element at the specified
indexy removeAllElements() empties out the vector
-
8/6/2019 Java Day23 Swings
18/23
-
8/6/2019 Java Day23 Swings
19/23
-
8/6/2019 Java Day23 Swings
20/23
Here we set up the
column-header
vector by adding
strings to it.
There will be a totalof four columns in
this example.
-
8/6/2019 Java Day23 Swings
21/23
-
8/6/2019 Java Day23 Swings
22/23
In order to ensure
that the JTable is
scrollable, we put itinto a JScrollPane.
We add the
JScrollPane to the
frame instead of
directly adding theJTable to the frame.
-
8/6/2019 Java Day23 Swings
23/23