Today's Objectives - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Today's Objectives

Description:

Learn the basics of Java event processing and Swing. ... 'display button' to the form and bind the button to the appropriate event handler. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 19
Provided by: ucd76
Category:
Tags: bind | objectives | today

less

Transcript and Presenter's Notes

Title: Today's Objectives


1
Today's Objectives
  • Understand how a Java application differs from a
    Java applet.
  • Learn the basics of Java event processing and
    Swing.
  • Learn how to use JDeveloper to create an
    applet/Java application.

2
Applet
  • Applets display AWT objects in an Applet. Applet
    is a subclass of the Container class.
  • Applets execute within an Internet Browser or
    Appletviewer.
  • An applet tag is used to embed a reference to the
    applet in a .html file e.g. myPage.html.

3
Java Application
  • Java applications display AWT objects in a Frame.
    Frame is a subclass of the Container class.
  • Java applications are executed by the Java
    interpreter.
  • java applicationname

4
AWT Overview
Label
Container
Text field
First
Panel
Last
ID
Canvas
E-Mail
Check box
ISMG 6020
Start Browse
Button
ISMG 6140
Modify
5
Container for My Dept EJB
6
Applet Structure
  • Applet is a type of panel (container)
  • Applet adds AWT/Swing components to itself.
  • Sample Code to Establish the Display Button
  • XYLayout xYLayout1 new XYLayout()
  • JButton displayButton new JButton()
  • displayButton.setText("Display")
  • this.getContentPane().add(displayButton, new
    XYConstraints(105, 294, 107, -1))

7
JDeveloper
  • Select layout manager type.
  • Select interface component style and type AWT
    button, SWING textfield, AWT checkbox, etc.
  • Click on panel to insert interface component into
    applet. Use mouse to move and stretch component.
  • Click on the interface component. In the
    property list dialog box, set values for the
    interface component.
  • e.g., displayButton is object name
  • "Display" is text value of displayButton
  • Drag displayButton to desired position (as
    aforementioned)

8
Layout Managers
  • Absolute Positioning (none) arranges components
    by pixel position. Demo
  • Box Layout either stacks its components on top of
    each other (with the first component at the top)
    or places them in a tight row from left to right
    -- your choice. Demo
  • Flow layout arranges components from left to
    right in rows. The rows are aligned left, right,
    or centered. Works like a typewriter. Demo
  • Grid Layout arranges components in a table of
    equal-size cells. Rows are filled top to bottom,
    columns left to right. Demo
  • Border Layout arranges components by geographic
    position (North, South, East, West) Demo
  • e.g., add("North", new Button("First"))
  • Card Layout arranges components by a tabbed card
    deck. Demo
  • Grid Bag Layout arranges components in a grid.
    Components are placed in cells as specified by
    cell coordinates, not pixel coordinates. Demo

9
AWT/Swing Methods for Developing Event Handlers
10
Integer Manipulation
  • Accept an integer value from the user
  • Int id (Integer.valueOf(textField.getText().tri
    m())).intValue()
  • Display an integer value to the user
  • textField.setText(String.valueOf(result))

11
FYI Simple Event Handling JDK 1.1
  • The action method in JDK 1.02 is replaced with
    actionPerformed method.
  • public void actionPerformed(ActionEvent e)
  • Object object event.getSource()
  • if (object button1)
  • onButton1()
  • else if (object button2)
  • onButton2()

12
Advanced Event Handling in JDK 1.1
  • Choose a listener object (applet, button, etc.)
    for a GUI event which the applet wants to
    monitor, e.g. button, mouse, etc. The listener
    object contains methods for handling all events
    associated with the GUI object.
  • In the UI Editor open the applet,
  • Double click on the button to create and register
    the listener for that event.
  • When the event occurs, the event handler of the
    listener object is invoked directly.

13
In-Class Demo
  • Create Applet
  • Create HTML file to run the applet
  • Connect to EJB
  • Create simple form for accepting dept id.
  • Add a display button to the form and bind the
    button to the appropriate event handler.

14
Add Department Popup Dialog Box
15
Dialog Box
  • Develop a new class derived from JDialog.
  • Use GUI builder and a Layout manager to define
    the form. Add Ok and Cancel buttons.
  • Set properties visible true and modal true
    for the JDialog form.
  • Add public attributes to the JDialog derived
    class for exchange of data.
  • Program OK button listener to copy the user input
    to the public attributes of the JDialog class.

16
addDeptDlgBox
  • public class addDeptDlgBox extends JDialog
  • public long deptNo
  • public String dname
  • public String loc
  • public boolean OK
  • XYLayout xYLayout1 new XYLayout()
  • JButton cancelButton new JButton()
  • JButton okButton new JButton()

17
Action Listeners for addDeptDlgBox Buttons
  • void OKButton_actionPerformed(ActionEvent e)
  • deptNo (Integer.valueOf(this.deptNoTxtField.getT
    ext().trim())).intValue()
  • dname this.dnameTxtField.getText()
  • loc this.locTxtField.getText()
  • OK true
  • setVisible(false)
  • void CancelButton_actionPerformed(ActionEvent
    e)
  • setVisible(false)
  • OK false

18
Applet Use of Dialog Box
  • void AddButton_actionPerformed(ActionEvent e)
  • addDeptDlgBox dlb new addDeptDlgBox()
  • dlb.setModal(true)
  • dlb.setVisible(true)
  • if (dlb.OK)
  • try dept deptHome.create( dlb.deptNo )
  • dept.setDname(dlb.dname)
  • dept.setLoc(dlb.loc)
  • catch (Exception ex) labelErrorMsg.setVisible
    (true)
Write a Comment
User Comments (0)
About PowerShow.com