1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
1 Object-Oriented Programming (Java), Unit 20 Kirk Scott.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Intro to Object-Oriented (“OO”) Design. OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Android 4: Creating Contents Kirk Scott 1. Outline 4.1 Planning Contents 4.2 GIMP and Free Sound Recorder 4.3 Using FlashCardMaker to Create an XML File.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Java Programming Chapter 10 Graphical User Interfaces.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
4.1 Instance Variables, Constructors, and Methods.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Object-Oriented Programming (Java), Unit 26 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
1 Object-Oriented Programming (Java), Unit 24 Stand In Final Project Description Kirk Scott.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Creating a GUI Class An example of class design using inheritance and interfaces.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Android 3: Exploring Apps and the Development Environment
Object-Oriented Programming (Java), Unit 22
Object-Oriented Programming (Java), Unit 23
Java Programming: From Problem Analysis to Program Design,
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
Constructors, GUI’s(Using Swing) and ActionListner
Topics Introduction to File Input and Output
CMSC 202 Exceptions.
Presentation transcript:

1 Object-Oriented Programming (Java), Unit 23 Kirk Scott

2 Serializability, File Choosers, and Multiple Frames 23.1 Serializability 23.2 File Choosers 23.3 Multiple Frames

Serializability

4 ClickSave This example program adds two more menu options to the example One triggers a listener to save the current state of the cups in the application The other triggers a listener to reload them. Saving and loading will involve the concept of serializability. A screenshot of the application, showing the menu, is given on the next overhead.

5

6 The Serializable interface Serializability is the term used to describe tools in the Java API that make it possible to save objects in files. To be serializable, a class has to implement the Serializable interface. If a class implements the interface, it is possible to save and load complete objects of a class. It is not necessary to take them apart and save their instance variable values one at a time, for example.

This interface does not require the implementation of any methods. In other words, it is a marker interface, kind of like Cloneable was a marker interface. Certain requirements have to be met in order for a class to successfully implement the interface. In particular, if a class is to be serializable, every object that it contains also has to be serializable. 7

8 The Cup and Seed classes: The SeedCup class implements the Serializable interface. The SeedCup class has a Rectangle instance variable. The Rectangle class in the Java API implements the Serializable interface.

It is worth noting that the Rectangle2D.Double class in the Java API does not implement the interface. This provides a concrete example of something that you could not include in a serializable class of your own. The SeedCup class would not be serializable if it were based on Rectangle2D.Double instead of Rectangle. 9

The SeedCup class contains an ArrayList that will hold instances of the Seed class. In order for the SeedCup class to be serializable, it will also be necessary for the Seed class to be serializable. It is, and that will be discussed next. 10

Because every object that an instance of a SeedCup class could contain is serializable, the SeedCup class is serializable. All that has to be done, and the only change from the previous version of the SeedCup class, is to declare it serializable. public class SeedCup implements Serializable 11

The Seed class also implements the Serializable interface. Unlike with the rectangle classes, none of the system supplied ellipse classes implement the Serializable interface. This accounts for one of the important design characteristics of the Seed class which was already built in earlier, without giving a full explanation of why. 12

Seeds do not have instances of ellipses as instance variables. The application code uses elliptical dots to represent seeds, but they are generated and displayed as part of the code of the drawSeed() method. The dots are not persistent. They are only transient visual representations of seeds. 13

This is a design point worth noting. The application would not support serializability if the dots (ellipses) were instance variables of the Seed class. All that has to be done, and the only change from the previous version of the Seed class, is to declare it serializable. public class Seed implements Serializable 14

The SaveListener: The SaveListener is built around the concept of serializability. It is possible to open an output stream to a file which is designed to handle the saving of serializable objects. The user is prompted for the name of a file. 15

An ObjectOutputStream is constructed. Then, one after the other, references to the objects comprising the state of the application are acquired by calling get methods. As they are acquired, they are saved by calling the write() method on the output stream. 16

The SaveListener has to deal both with the cups and the text area. The getText() method returns a String, which is also an object. This method contains the logic for acquiring the text for the action record area. 17

You may or may not be familiar with file I/O in Java. Notice that I/O operations have to occur in a try/catch block. File I/O operations are a classic example of exception handling in Java. 18

An I/O error might occur that is not the program’s fault The exception handling mechanism makes it possible for the program to handle the error gracefully, assuming the programmer wants to go to the trouble of writing the code to do so. The code for the SaveListener is given on the following overheads. 19

20 private class SaveListener implements ActionListener { public void actionPerformed(ActionEvent event) { String inputString; inputString = JOptionPane.showInputDialog("Enter file path name for saving.");

21 try { ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(inputString)); objOut.writeObject(myPanel.getMyCupA()); objOut.writeObject(myPanel.getMyCupB()); objOut.writeObject(myPanel.getWhichCupIsActive()); objOut.writeObject(myPanel.getText()); objOut.writeObject(myPanel.getMoveCount()); } catch(IOException e) { System.out.println("Problem making or writing to an output stream."); System.out.println(e); }

Notice that a type of “auto-boxing” occurs on the moveCount value. The system automatically wraps the integer value in an instance of the Integer class. It’s automatic when saving. It turns out that it will have to be dealt with directly when loading. 22

23 The LoadListener: The LoadListener is analogous to the SaveListener. It is also built around the concept of serializability. It is possible to open an input stream to a file which is designed to handle the reading of serializable objects which were previously saved in the file.

In the LoadListener the user is prompted for the name of a file. The current state of the application is disposed of by removing the current panel. A new panel is created and added to the content pane. 24

The ObjectInputStream is constructed. The file containing the SeedCup objects is read from the beginning. The objects are retrieved in the order in which they were saved. This is done using the readObject() method. Notice that an Object reference is returned, and this has to be cast to the class expected. 25

The retrieved objects are put into the application panel. This is done using set methods in the application. The application deals with cups and text. To set the text area with text retrieved with the load operation, call setText() with the String that is returned from the file. 26

Reading is also a file I/O operation, so it also has to occur in a try/catch block. It is worth repeating that the order of the logic for retrieving objects is the same as for saving them. Do not get the mistaken idea that something odd happens, like the order is reversed, for example. 27

private class LoadListener implements ActionListener { public void actionPerformed(ActionEvent event) { String inputString; inputString = JOptionPane.showInputDialog("Enter file path name for loading."); 28

29 try { Container contentPane = getContentPane(); contentPane.remove(myPanel); myPanel = new ClickSavePanel(); contentPane.add(myPanel, "Center"); ObjectInputStream objIn = new ObjectInputStream( new FileInputStream(inputString)); myPanel.setMyCupA((SeedCup) objIn.readObject()); myPanel.setMyCupB((SeedCup) objIn.readObject()); myPanel.setWhichCupIsActive((SeedCup) objIn.readObject()); myPanel.setText((String) objIn.readObject()); Integer localMoveCountObject = (Integer) objIn.readObject(); int localMoveCount = localMoveCountObject.intValue(); myPanel.setMoveCount(localMoveCount); setVisible(true); } catch(IOException e)…

File Choosers

ClickChooser This example program makes use of a JFileChooser in the SaveListener and the LoadListener. The screenshot on the next overhead shows what appears if the Save option is taken in the menu. It should be apparent that the Java API makes available useful tools for creating a graphical file I/O interface. 31

32

33 The lines of code given below appear in the ClickChooserFrame class and its constructor, respectively. This is where the chooser is created that the menu item listeners make use of. JFileChooser myChooser; … myChooser = new JFileChooser();

34 The SaveListener and the LoadListener: Inside the actionPerformed() method of the save and load listeners the following will be found: –The call to setCurrentDirectory() will cause the chooser to open the home directory of the running application. –The call to showSaveDialog() causes the chooser to be displayed. –The call to getSelectedFile().getPath() obtains the full path name of the file which the user selects or enters.

Only key parts of the code are shown for this new version of the application. Most of the code remains the same as in the previous version. Parts of the save listener and the load listener are given on the following overheads. 35

36 Key parts of the SaveListener: private class SaveListener implements ActionListener { public void actionPerformed(ActionEvent event) { … myChooser.setCurrentDirectory(new File(".")); myChooser.showSaveDialog(ClickChooserFrame.this); fileName = myChooser.getSelectedFile().getPath(); …

37 Key parts of the LoadListener: private class LoadListener implements ActionListener { public void actionPerformed(ActionEvent event) { … myChooser.setCurrentDirectory(new File(".")); myChooser.showOpenDialog(ClickChooserFrame.this); fileName = myChooser.getSelectedFile().getPath(); …

38 Slow construction of instances of JFileChooser There is an unpleasant potential interaction between at least some versions of the Windows operating system and instances of JFileChooser in Java. The construction of a JFileChooser may take a very long time if there are any zipped folders on the desktop of the machine you are running on.

In order to get reasonable performance of the Java code, move any zipped folders into another, unzipped folder. This is very strange, and it's enough to make you cry, but at least there is an easy solution that doesn't involve elaborate changes to your code. 39

Multiple Frames

41 ClickMany This program makes it possible to open up more than one frame with cups in it. The menu for the main frame allows you to open up the new frames or exit the application overall. The menu for a subframe allows you to restart or close that frame—without exiting the application. Screenshots of the main frame and a subframe, and their menu options, are given on the following overheads.

42

43

44 The application is getting large enough that it’s hard to fit everything into a single static structure diagram that would fit on a single overhead. Therefore, there are two UML diagrams for this application. The diagram on the following overhead shows the relationships of the classes associated with the main frame of the program.

45

46 The second UML diagram for the application shows relationships of the classes associated with the subframes of the program. Notice how the application panel, cups, and seeds are unchanged from the previous versions of the application, but they now descend from the subframe rather than the main frame.

The structure of the menu belonging to the subframe is analogous to that of the main frame, but the menu items have different names and their listeners implement the corresponding functionality. This application still has key handling functionality, but in order to keep the diagrams a little simpler, it is not shown in them. The second UML diagram is given on the following overhead. 47

48

49 Portions of the code for the application follow. Notice first of all that the main frame is simplified somewhat by removing the WindowCloser and going back to the default close operation. When the main frame is closed, the application ends. class ClickManyFrame extends JFrame … setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

50 The main frame class has a listener for the menu option to make a subframe. This code is shown next. Notice how each new subframe is located at an offset from the previous one so they don’t completely overlap each other on the screen.

51 private class MakeSubFrameListener implements ActionListener { public void actionPerformed(ActionEvent event) { ClickSubFrame myframe = new ClickSubFrame(); myframe.setLocation(subFrameLocation, subFrameLocation); if(subFrameLocation >= 500) { subFrameLocation = 50; } else { subFrameLocation += 50; } myframe.setVisible(true); }

52 The subframe of this application is similar to the main frames of the previous example applications. The subframe has menu items for restarting and closing. The RestartListener implements the restarting of the application by renewing the cups in the panel. Code for this is given on the following overhead.

53 private class RestartListener implements ActionListener { public void actionPerformed(ActionEvent event) { myPanel.renewBoard(); myPanel.renewTextArea(); myPanel.setMoveCount(1); }

The CloseListener for the subframe does something different from the WindowCloser or Exit menu options for the main frame. Closing a subframe does not end the application. It merely disposes of that particular subframe. Code for this is given on the following overhead. 54

55 private class CloseListener implements ActionListener { public void actionPerformed(ActionEvent event) { dispose(); }

The renewBoard() method in the panel class duplicates all of the logic of the constructor, plus a call to repaint(). Code for this is given on the next overhead. 56

57 public void renewBoard() { myCupA = new SeedCup(4, 200, 200, 40, 40); myCupB = new SeedCup(0, 250, 200, 40, 40); whichCupIsActive = myCupA; repaint(); }

Code for the renewTextArea() method is given on the next overhead. 58

public void renewTextArea() { actionRecordArea.setText(""); repaint(); } 59

Code for the setMoveCount() method is given on the next overhead. 60

public void setMoveCount(int countIn) { moveCount = countIn; } 61

Managing Increasing Complexity As applications grow in complexity it can become difficult to manage all of the pieces and keep them all working. In the last unit keystroke functionality was introduced. You may notice that in later versions of the application that is removed. Making keystrokes work when other functionality is added can be difficult. In the assignments, keystroke functionality can be dropped after unit 22 until it’s raised again in a future unit. 62

63 The End