Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Software Engineering

Similar presentations


Presentation on theme: "Object-Oriented Software Engineering"— Presentation transcript:

1 Object-Oriented Software Engineering
Java Exceptions, Dialog Boxes, and Icons

2 Contents Error Handling Exceptions Dialogs Option Dialogs Icons

3 Error Handling Best way to handle errors is to avoid them in the first place. Java is designed to be modular to separate out functionality. However modularity can be broken if fields are not protected. public class MyClass { Double dbl; } public class RogueClass { MyClass mc = new MyClass(); public void make_trouble (String x) { mc.dbl = new Double(x); make_trouble("shoo") will result in an error. There is no type checking to prevent this.

4 Error Handling Fields (variables) should be declared as private normally. That makes them inaccessible directly from any other class. set and get methods should be defined for any fields that are to be accessed from other classes. When this is inconvenient fields should be declared protected. That is only to be accessed from sub-classes and classes in the same package.

5 Error Handling Note this is not valid UML, its just to illustrate the idea SomeClass private JFrame frame; protected Double dbl; get_frame() set_frame()

6 Exceptions public class MyClass { private Double dbl;
public set_dbl (String st) { try { dbl = new Double(st); } catch (NumberFormatException exp1) { System.out.println("OOOPS: " + e1.toString() + "\n"); } public get_dbl () {return dbl;} If you're not sure what type of exception to expect force an error in the code. The compiler will helpfully display an error message including the exception type. TIP

7 Other Exceptions try { icon_file_name = file.getCanonicalPath();
Certain methods throw exceptions. In this case the compiler will not allow those methods to be used unless the exceptions are caught. try { icon_file_name = file.getCanonicalPath(); } catch (IOException exp0) { System.out.println("IOException : \n" + e1.toString() + "\n"); } Note body of catch statement can be anything. Even empty.

8 Dialogs string_age = stringAge; try { age = new Double(stringAge);
The following code taken from the PersonMaker.java application. The full code is on the course web site. With user provided values, rather than print error messages, can use dialogs to correct values. public void setAge (String stringAge) { string_age = stringAge; try { age = new Double(stringAge); } catch (NumberFormatException exp1) { string_age = handleTextInput("Age"); age = new Double(string_age); }

9 Dialogs String s = (String)JOptionPane.showInputDialog( null,
Useful bit of code to cope with capturing user input for simple values: public String handleTextInput (String dis_text) { String s = (String)JOptionPane.showInputDialog( null, dis_text, "PersonMaker Class", JOptionPane.PLAIN_MESSAGE, "Fluffy"); return s; }

10 Dialogs JOptionPane.showInputDialog Parameters:
parentComponent - the parent Component for the dialog message - the Object to display title - the String to display in the dialog title bar messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE icon - the Icon image to display selectionValues - an array of possible selections initialSelectionValue - the value used to initialize the input field

11 Message Dialogs Simple alerts can also be shown as dialogs.
JOptionPane.showMessageDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE);

12 Input Dialogs Simple input dialogs can also be shown with little code:
JOptionPane.showInputDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE);

13 Input Dialogs Simple input dialogs with optional argument
can also be shown with little code: JOptionPane.showInputDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, null, "Fluffy");

14 Code to implement option pane
import java.awt.*; import javax.swing.*; import javax.swing.JOptionPane; import javax.swing.JDialog; public class SimDialog { public static void main(String[ ] args) { JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, "Fluffy"); }

15 Using Icons in dialogs Icon is an image of fixed size that can be used to decorate most Swing components in some way or other. For any class named, say, Foo we can add method to define icons using java.net.URL (taken from Java Tutorial). return value type protected ImageIcon createImageIcon(String path, String description) { java.net.URL imgURL = Foo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); return null; } Every class is derived from Object class. Hence can use methods and fields of that class.

16 Using Icons in dialogs Can now add Icons as decoration to dialog boxes. Full code is in file SimIcon.java public static void main(String[ ] args) { ImageIcon dragon = createImageIcon("images/black-dragon.gif", "Black Dragon"); JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, dragon, "Fluffy"); }

17 Using Icons in dialogs

18 Lists of Options in Dialog
To make the user choose one of a fixed number of alternatives can invoke showInputDialog with an array of allowed values. Can instantiate a finite array of known values very simply: String[ ] arrayOfOpts = { "Fee", "Fi", "Fo", "Fum" }; Normally when array holds unknown values is created with new statement like all other fields: float[] arrayOfFloats = new float[10];

19 Lists of Options in Dialog
Putting it together we can create SimOpts.java application. This file can be found in code directory for this lecture. public static void main(String[ ] args) { ImageIcon dragon = createImageIcon("images/black-dragon.gif", "Black Dragon"); String[] arrayOfOpts = { "Fee", "Fi", "Fo", "Fum" }; JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, dragon, arrayOfOpts, null); }

20 Lists of Options in Dialog

21


Download ppt "Object-Oriented Software Engineering"

Similar presentations


Ads by Google