CASE STUDY: Applet Development with GUI and Client-side File Input Arkadiusz Edward Komenda.

31
CASE STUDY: Applet Development with GUI and Client-side File Input Arkadiusz Edward Komenda
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    226
  • download

    0

Transcript of CASE STUDY: Applet Development with GUI and Client-side File Input Arkadiusz Edward Komenda.

CASE STUDY:Applet Development with GUI and Client-side File Input

Arkadiusz Edward Komenda

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.

Certificates and Files on Client not recommended!

• A signed jar file is not the best solution to this problem. It is necessary to access files on the client. But the server is a better place to keep files. A separate tutorial is provided to show you how to access files on the server using the Eclipse IDE.

• However, this tutorial is a great introduction to several of the challenges involved in Java programming. It can give you a head start, as well as a reference when the topics are covered.

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.

Problem Statement

Write a Java applet to calculate the average of a list of integers and their count. The input data is stored in a text file, in which each line contains a single integer. The input file is chosen via Open File Windows-like Dialog box. The average and count (number of integers in a file) are displayed within the applet. Provide for error checking of the file name, i.e. the program should display a meaningful message when the input file name is incorrect, and it should ignore any lines in the input file that are other than a single integer.

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

Class Definition

• Declare class AverageCount • Extend it as an Applet Event Handling• Remember to import packages supporting

applets and Abstract Window Toolkit (AWT) for GUI

1

2

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 Buttons, Labels, TextFields, Checkboxes, etc.

Panels, Buttons, Labels (1)

• Three panels are declared:– mainPanel – holds three Labels 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 Labels which will display the results (resultLabel, resultLabel2)

Panels, Buttons, Labels (2)

buttonPanel

displayPanel

mainPanel

openButton

mainLabelmainLabel2mainLabel3

resultLabelresultLabel2

Panels, Buttons, Labels (3)

1

23

4

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

2

1

Event Handling (2)

• openButton is tied to ActionListener

• actionPerformed method defines what actions should be taken once Open Button is clicked

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

123

File Input – The Applet

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

123

Average Calculation and Count

• Calculation of the average of the integers and their count is straight forward

1

2

Exception Handling - Filename

• Error checking for the correctness of file name is handled by the use of try and catch statements

• When the file name is incorrect an appropriate message is displayed in resultLabels

Exception Handling - isOkInt

• Testing whether the read data from input file is an integer is done by isOkInt(String s) method, which is left for students to implement

JAR• JAR is a packing utility similar to ZIP or TAR• We can pack numerous files (.class, .gif, and others)

into a single compressed archive file (filename.jar)• When an HTML page loads an applet and encounters

filename.jar file (specified between <APPLET></APPLET> tags) it uploads the whole file in one connection, thus saving connection time

• To create a JAR file:jar cvf AverageCount.jar AverageCount.class

Signing Applet• For an applet to access local files (stored on user’s local machine)

the applet has to be in the form of a JAR file (AverageCount.jar) which has your digital certificated attached to it.

• In short, a JAR file has to be signed• Before signing a JAR file, you must make your own digital

certificate– At the prompt type:keytool -genkey -keyalg rsa -alias mykey keytool -export -alias yourkey -file mycertificate.crt

• mycertificate.crt is a file with your digital certificate• Now to sign AverageCount.jar file

– jarsigner AverageCount.jar mykey

Applet Deployment

• Place AverageCount.jar (after signing it) on the HTTP server (AFS server at NJIT)

• In your .html file (e.g. AverageCount.html) include the following line in the place where you want your applet to appear on a web page<APPLET code="AverageCount.class" archive="AverageCount.jar" width=420 height=280></APPLET>

Complete Program (1)

Complete Program (2)

Complete Program (3)

Complete Program (4)

Complete Program (5)

References

• Jia, Xiaoping. Object Oriented Software Development using Java- 2nd edition, Adison Wesley 2003

• http://java.sun.com/reference/docs/index.html• http://java.sun.com/j2se/1.5.0/docs/api/index.html• http://java.sun.com/docs/books/tutorial/uiswing/TO

C.html