15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling
Mar 27, 2015
15Copyright © 2005, Oracle. All rights reserved.
Adding User Interface Componentsand Event Handling
15-2 Copyright © 2005, 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
15-3 Copyright © 2005, Oracle. All rights reserved.
Swing Components
• Text controls– JTextField– JPasswordField– JTextArea– JEditorPane– JTextPane
• Graphic controls– JTree– JTable– JToggleButton
15-5 Copyright © 2005, Oracle. All rights reserved.
Swing Components in JDeveloper
Use the Swing Component Palette to add items.
15-7 Copyright © 2005, Oracle. All rights reserved.
System Navigator
Invoking the UI Editor
Right-click and select Open from the Context menu.
Code Editor
Context menu
15-8 Copyright © 2005, Oracle. All rights reserved.
1: Open the Component Palette; select the Swing category.
2: Drag the component to the form. The class updates automatically.
How to Add a Component to a Form
15-9 Copyright © 2005, Oracle. All rights reserved.
Change property values in the Inspector.
Edit the Properties of a Component
15-10 Copyright © 2005, Oracle. All rights reserved.
Code Generated by JDeveloper
For example: Adding a JButton to a Jframe:
import javax.swing.JButton;
public class JFrame1 extends JFrame {
JButton jButton1 = new JButton();
...
public void jbInit() throws Exception {
...
jbutton1.setText("jButton1");
...
this.getContentPane().add(jButton1,
new XYConstraints(21, 20, 118, 48));
}
15-12 Copyright © 2005, Oracle. All rights reserved.
Creating a Menu
• Select “Create Menu Bar” during application creation.
• Add a 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.
• A JMenuBar can be added to any top-level container, such as Frames, Dialogs, or Applets.
15-13 Copyright © 2005, Oracle. All rights reserved.
Using JDeveloper Menu Editor
• In the JDeveloper Menu Editor, use the Structure pane:– Expand the Menu node.– Click the menu bar object for a visual
representation.– Right-click menu or menu items to alter
structure from the Context menu options.
Clickmenu barobject in
Structure paneto displaymenu bar
Context menu when
right-clicking a menu item
15-14 Copyright © 2005, Oracle. All rights reserved.
Practice 15-1: Overview
This practice covers:
• Creating the OrderEntryMDIFrame menu
• Adding the menu items and a separator to the Order menu
• Adding components to OrderEntryFrame to form its visual structure
15-15 Copyright © 2005, Oracle. All rights reserved.
UI for Java Application
15-20 Copyright © 2005, 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.
15-21 Copyright © 2005, Oracle. All rights reserved.
Event Handling Code Basics
• Create the event source.
• Create the event listener implementing the required event interface.
• Register the listener with the event source.
Jbutton findBtn = new Jbutton("Find");
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// handler logic
}
}
findBtn.addActionListener(new MyListener());
15-22 Copyright © 2005, Oracle. All rights reserved.
OK
SourceEvent listener object
Handler method
Event Handling Process: Registration
MyListener actionListenerObj = new MyListener();
public void jbInit() {
button1.addActionListener(actionListenerObj);
…
}
15-24 Copyright © 2005, Oracle. All rights reserved.
Event Handling Process:The Event Occurs
Notified
public void jbInit() {
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Your code to handle the ActionEvent
}
}); … }
OK
SourceEvent listener object
Handler method
15-25 Copyright © 2005, Oracle. All rights reserved.
Event Handling Process:Running the Event Handler
SourceOK
SourceEvent listener object
Handler method:save changes and quit
15-26 Copyright © 2005, Oracle. All rights reserved.
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
}
Using Adapter Classes for Listeners
Adapter classes are “convenience” classes that implement event listener interface:• They provide empty method implementations.• They are extended and the desired method
overridden.
15-27 Copyright © 2005, Oracle. All rights reserved.
Swing Model View Controller Architecture
• Model, View, Controller principles
• Terms explained:– Model represents the data or information.– View provides a visual representation of the data.– Controller handles events modifying the
view/model.
• Always update Swing components on the event thread queue, or use SwingUtilities methods.
Model
Controller ViewViewEventmodify
modify Notify update
Get changed data
15-30 Copyright © 2005, 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 done in the event handling thread.
Note: Consult the Java API documentation for details about each component’s capabilities.
15-31 Copyright © 2005, 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 DefaultListModel class for the model.
• Object getSelectedValue()– Returns the selected object, or null if nothing is
selected• int getSelectedIndex()
– Returns the index of the selected item, or –1 if nothing is selected
15-32 Copyright © 2005, Oracle. All rights reserved.
Events that a component can generate
Event handler methods
What Events Cana Component Generate?
15-33 Copyright © 2005, Oracle. All rights reserved.
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.
How to Define an Event Handlerin JDeveloper
15-34 Copyright © 2005, Oracle. All rights reserved.
public void jbInit() throws Exception {
…
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
}
Find
Default Event Handling Code Style Generated by JDeveloper
15-35 Copyright © 2005, Oracle. All rights reserved.
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);
}
}
Completing the Event Handler Method
15-36 Copyright © 2005, 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 for the event to be
handled
• Create a menu bar with menus and menu items
• Handle events
15-37 Copyright © 2005, Oracle. All rights reserved.
Practice 15-2: Overview
This practice covers adding event handling for:
• Order > New menu
• Find Customer button
• Add Product button