Docsity

Applet Development - Java and the Web - Lecture Slides, Slides of Java Programming

Toggle sidebar
bg1
CASE STUDY:
Applet Development with GUI and Client-
side File Input
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Applet Development - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

CASE STUDY:

Applet Development with GUI and Client-

side File Input

Professor Blank’s Introduction

  • I asked Mr. Komenda to write this case study of problem 4.2 in the text because he did an effective job of solving several problems that are required to create an applet that reads files, when all the other students in the class had to write an application instead of an applet. He had to solve several problems before the related material was covered in the text.

Problems to be solved:

• This early in the course (Chapter 4), most

students cannot use:

  • GUI Panels
  • File Dialog Boxes
  • Exception Handling
  • File I/O
  • Java Jar Files
  • Certificates

Special notes

  • It would have been more appropriate in this case to put the file of numbers to be counted and averaged on the AFS server in the same directory from which the applet is loaded instead of on the user’s disk. Then the certificate should not be necessary.
  • Also, this uses a self-published certificate instead of one from a certificate authority, so it requires the user to trust the developer of the certificate.

Application or Applet

  • One of the key differences between an application and an applet is start-up. Every application needs a main method, which tells the program where to start.
  • Applets cannot have a main method. Every applet needs at least one of these methods: init , start , or paint.
  • The browser uses init to notify the applet that it has been loaded and start to tell the applet that it can begin executing. Anything displayed in the applet must use a paint method.

Outline

  • Graphical User Interface (GUI)
  • Event Handling
  • File Input
  • Processing (average and count calculation)
  • Exception Handling
  • JAR and Signing Applet

Graphical User Interface (GUI)

  • Our Applet GUI consists of Panels
  • Panel for our purpose can be imagined as a rectangular plane ( container ) where it is possible to place Button s , Label s , TextField s , Checkbox es, etc.

Panels, Buttons, Labels (1)

  • Three panels are declared:
    • mainPanel – holds three Label s with information and instructions to the user about the program ( mainLabel, mainLabel2, mainLabel3 )
    • buttonPanel – holds a Button that will activate Open File Dialog box and after selection of an input file the applet will proceed with calculation of the average ( openButton )
    • displayPanel – holds two Label s which will display the results ( resultLabel, resultLabel2 )

Panels, Buttons, Labels (3)

Event Handling (1)

  • When user clicks on Open Button , the applet opens a dialog box to select an input file
  • class AverageCount implements ActionListener interface which contains one method actionPerformed
  • Remember to import java.awt.event.* package

Event Handling – Our Applet

File Input – Dialog Box

  • Define new Frame to hold Open File Dialog box
  • Create Open File Dialog box ( FileDialog ) in the Frame
  • Once the user clicks on Open Button , then actionPerformed method calles doOpen() to display ( setVisible ) Open File Dialog box

1 2 3

File Input – Access

  • Define FileReader and BufferedReader to open the selected file and read data from it
  • File name and file path are stored in File type object and are passed to the FileReader
  • Actual reading of data is performed by BufferedReader via .readLine() method

1 2 3

Average Calculation and Count

  • Calculation of the average of the integers and

their count is straight forward

1

2