You are on page 1of 44

16

Adding User Interface Components


and Event Handling

Copyright © 2007, Oracle. All rights reserved.


Objectives

After completing this lesson, you should be able to do


the following:
• Add Swing components to an application
• Get and modify the contents of the components
• Provide event handlers for common types of
events
• Create a menu bar

16-2 Copyright © 2007, Oracle. All rights reserved.


Swing Components

• Text controls
– JTextField
– JPasswordField
– JTextArea
– JEditorPane
– JTextPane
• Graphic controls
– JTree
– JTable
– JToggleButton

16-3 Copyright © 2007, Oracle. All rights reserved.


16-4 Copyright © 2007, Oracle. All rights reserved.
Swing Components in JDeveloper

Use the Swing Component Palette to add items.

16-5 Copyright © 2007, Oracle. All rights reserved.


16-6 Copyright © 2007, Oracle. All rights reserved.
Invoking the UI Editor
Right-click and select Open from the shortcut menu.

UI Editor
Applications
Navigator Shortcut menu

16-7 Copyright © 2007, Oracle. All rights reserved.


Adding a Component to a Form
1. Open the Component Palette
and select the Swing
category.

2. Drag the component to the form. The


class updates automatically.

16-8 Copyright © 2007, Oracle. All rights reserved.


Editing the Properties of a Component

Change property values in


the Property Inspector.

16-9 Copyright © 2007, Oracle. All rights reserved.


Code Generated by JDeveloper

Example: Adding JButton to JFrame


import javax.swing.JButton;
public class JFrame1 extends JFrame {
private JButton jButton1 = new JButton();
...
public void jbInit() throws Exception {
this.setLayout(null);
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(25, 140,
73, 22));
this.add(jButton1, null);
}

16-10 Copyright © 2007, Oracle. All rights reserved.


16-11 Copyright © 2007, Oracle. All rights reserved.
Creating a Menu

• Select Create Menu Bar during application


creation.
• Add JMenuBar from the Component Palette.
• JDeveloper creates:
– JMenuBar for visual container for menus
– JMenu, which represents a menu of items added to
a menu bar
– JMenuItems, which are placed in a JMenu
• Each JMenuItem supports events, interfaces, and
handler methods in the same way as with other
Swing UI components.
• JMenuBar can be added to any top-level container,
such as frames, dialog boxes, and applets.
16-12 Copyright © 2007, Oracle. All rights reserved.
Using the JDeveloper Menu Editor

In the Structure pane of the JDeveloper Menu Editor:


1. Expand the Menu node.
2. Click the menu bar object for a visual
representation.
3. Right-click menu or menu items to display
shortcut menu options.

Shortcut menu
Click
when
menu bar
right-clicking a
object in
menu item
Structure pane
to display
menu bar.

16-13 Copyright © 2007, Oracle. All rights reserved.


Practice 16-1 Overview:
Adding User Interface Components

This practice covers the following topics:


• Creating the OrderEntryMDIFrame menu
• Adding menu items and a separator to the Order
menu
• Adding components to OrderEntryFrame to
create its visual structure

16-14 Copyright © 2007, Oracle. All rights reserved.


UI for the Order Entry Application

16-15 Copyright © 2007, Oracle. All rights reserved.


16-16 Copyright © 2007, Oracle. All rights reserved.
16-17 Copyright © 2007, Oracle. All rights reserved.
16-18 Copyright © 2007, Oracle. All rights reserved.
16-19 Copyright © 2007, Oracle. All rights reserved.
16-20 Copyright © 2007, Oracle. All rights reserved.
Java Event Handling Model

• How it works:
– Event originates from source and generates an
event object.
– An event listener hears a specific event.
– An event handler determines what to do.
• Setting it up:
1. Create an event source object.
2. Create an event listener object implementing an
interface with methods to handle the event object.
3. Write an event-specific method to handle the event.
4. Register the listener object with the event source
for the specified event.

16-21 Copyright © 2007, Oracle. All rights reserved.


Event Handling Code Basics

• Create the event source.


Jbutton findBtn = new Jbutton("Find");
• Create the event listener implementing the
required event interface.
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// handler logic
}
}

• Register the listener with the event source.


findBtn.addActionListener(new MyListener());

16-22 Copyright © 2007, Oracle. All rights reserved.


Event-Handling Process: Registration

Source
Event listener object

OK
Handler method

MyListener actionListenerObj = new MyListener();


public void jbInit() {
button1.addActionListener(actionListenerObj);

}

16-23 Copyright © 2007, Oracle. All rights reserved.


16-24 Copyright © 2007, Oracle. All rights reserved.
Event-Handling Process:
The Event Occurs

Source
Event listener object

OK
Notified Handler method

public void jbInit() {


button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Your code to handle the ActionEvent
}
}); … }

16-25 Copyright © 2007, Oracle. All rights reserved.


Event-Handling Process:
Running the Event Handler

Source
Event listener object

OK Handler method:
Source
save changes and quit

16-26 Copyright © 2007, Oracle. All rights reserved.


Using Adapter Classes for Listeners

Adapter classes are “convenience” classes that


implement event listener interfaces:
• They provide empty method implementations.
• They are extended, and the desired method is
overridden.
interface MouseListener {
// Declares five methods
}
class MouseAdapter implements MouseListener {
// Empty implementations of all five methods
}

public class MyListener extends MouseAdapter {


// Override only the methods you need
}

16-27 Copyright © 2007, Oracle. All rights reserved.


Swing Model-View-Controller Architecture

• Model-View-Controller principles
Event Controller
modify View
View

modify Notify update


Model
Get changed data
• Terms explained:
– Model represents the data or information.
– View provides a visual representation of the data.
– Controller handles events modifying the view or
model.
• Always update Swing components on the event
thread queue, or use SwingUtilities methods.
16-28 Copyright © 2007, Oracle. All rights reserved.
16-29 Copyright © 2007, Oracle. All rights reserved.
16-30 Copyright © 2007, Oracle. All rights reserved.
Basic Text Component Methods

• Text item (JLabel, JTextField, and JButton)


methods:
void setText(String value)
String getText()
• Additional methods in JTextArea:
void append(String value)
void insert(String value, int pos)

Changes to component contents are usually made
in the event handling thread.
Note: Consult the Java API documentation for details
about each component’s capabilities.

16-31 Copyright © 2007, Oracle. All rights reserved.


Basic JList Component Methods

Subset of JList component methods include:


• void setListData(Vector)
– Copies Vector to a ListModel applied with
setModel
• void setModel(ListModel)
– Sets model representing the data and clears
selection
– Uses the DefaultListModel class for the model
• Object getSelectedValue()
– Returns the selected object,
or returns null if nothing is selected
• int getSelectedIndex()
– Returns the index of the selected item,
or returns –1 if nothing is selected

16-32 Copyright © 2007, Oracle. All rights reserved.


What Events Can a
Component Generate?
Events that a component can
generate

Event handler
methods

16-33 Copyright © 2007, Oracle. All rights reserved.


How to Define an Event Handler
in JDeveloper

1. Select the event that you


want to handle.

2. Click the right column to fill in


a method name.

3. Double-click the right column to


create the method.

16-34 Copyright © 2007, Oracle. All rights reserved.


Default Event Handling Code Style
Generated by JDeveloper

public void jbInit() throws Exception {


… Find
findButton.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
findButton_actionPerformed(e);
}
}); …

void findButton_actionPerformed(ActionEvent e) {
// Your code to handle the ActionEvent
}

16-35 Copyright © 2007, Oracle. All rights reserved.


Completing the Event Handler Method

public class JFrame1 extends JFrame {



void findButton_actionPerformed(ActionEvent e){
// When the user clicks the button, display
// the list of customers in jTextArea1
String findList = (“Supplies-R-Us " + "\n" +
“Consulting Inc. " + "\n" +
“Just-In-Time Training ");
jTextArea1.setText(findList);
}
}

16-36 Copyright © 2007, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Add a Swing component to a visual container
• Get and modify the contents of the components
• Use the AWT event handling model to:
– Create an event source
– Create an event listener and handler code
– Register an event listener to handle the event
• Create a menu bar with menus and menu items
• Handle events

16-37 Copyright © 2007, Oracle. All rights reserved.


Practice 16-2 Overview:
Adding Event Handling

This practice covers adding event handling for:


• The Order > New menu
• The Find Customer button
• The Add Product and Remove Product buttons

16-38 Copyright © 2007, Oracle. All rights reserved.


16-39 Copyright © 2007, Oracle. All rights reserved.
16-40 Copyright © 2007, Oracle. All rights reserved.
16-41 Copyright © 2007, Oracle. All rights reserved.
16-42 Copyright © 2007, Oracle. All rights reserved.
16-43 Copyright © 2007, Oracle. All rights reserved.
16-44 Copyright © 2007, Oracle. All rights reserved.

You might also like