1 / 31

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

CASE STUDY: Applet Development with GUI and Client-side File Input. Arkadiusz Edward Komenda. Professor Blank ’ s Introduction.

royal
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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 OpenFile 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.

  7. 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.

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

  9. 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

  10. 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.

  11. 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 Labelswhich will display the results (resultLabel, resultLabel2)

  12. Panels, Buttons, Labels (2) mainLabel mainLabel2 mainPanel mainLabel3 resultLabel displayPanel resultLabel2 buttonPanel openButton

  13. Panels, Buttons, Labels (3) 1 2 3 4

  14. Event Handling (1) • When user clicks on Open Button, the applet opens a dialog box to select an input file • class AverageCountimplements ActionListener interface which contains one method actionPerformed • Remember to import java.awt.event.* package 1 2

  15. Event Handling (2) • openButton is tied to ActionListener • actionPerformed method defines what actions should be taken once Open Button is clicked

  16. Event Handling – Our Applet

  17. 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 methodcalles doOpen() to display (setVisible) Open File Dialog box 1 2 3

  18. File Input – The Applet

  19. 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

  20. Average Calculation and Count • Calculation of the average of the integers and their count is straight forward 1 2

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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>

  26. Complete Program (1)

  27. Complete Program (2)

  28. Complete Program (3)

  29. Complete Program (4)

  30. Complete Program (5)

  31. 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/TOC.html

More Related