Title: Miscellaneous Topics Announcements, Exceptions, and Dialogs
1Miscellaneous TopicsAnnouncements, Exceptions,
and Dialogs
2Test Wednesday
- The second test is Wednesday!!
- Closed book and notes.
- No calculators.
- 5-6 Questions.
- Includes Applets, GUIs, Loops, and Arrays.
3Project 5
- How is it going? Are you able to write a list of
your items out to a file? Can you read them back
in? - This weeks lab deals a bit with GUIs and
probably will provide you with some help in
building your GUI for this project.
4Basic Concept
- Build a panel which displays and controls one
item in your inventory. - Once that is built and looks okay go ahead and
create a bunch of them and add them to a layout.
5An Example from this Weeks Lab
- Stores information on an album
- Laid out as 4 portions, the albumInfo, the stock,
the box containing the quantity to buy, and a
button.
6Exceptions
- Java uses Exceptions to catch mistakes.
- Traditionally, we used to have to test error
conditions ourselves using if statements. - This worked but it was easy to overlook an item
that needed to be tested.
7Try, Catch, Finally, and Throw
- In using the I/O in Java we have seen a try-catch
block. - try // some statements
- catch (Exception e)
- // handle the exception
-
8There is another clause
- try
- // a group of statements catch (Exception e)
// The error handling code finally - // Done at the end regardless of
- // whether an exception occurred // or not.
-
9There Are All Kinds Of Exceptions
- Exception
- RuntimeException
- ArithmeticException
- ArrayStoreException
- ClassCastException
- IllegalArgumentException
- NumberFormatException
- IllegalMonitorStateException
- IndexOutOfBoundsException
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
- NegativeArraySizeException
- NullPointerException
- SecurityException
10Making You Own Exceptions
- You can build your own exceptions in Java to
signal when bad things happen. - To initiate or start the exception you use a
throw statement. - throw new Exception(Something is wrong!)
11Catching Multiple Exceptions
- Sometimes a block of code in a try-catch clause
will throw more than one type of exception. - Options
- Catch only an Exception (all Exceptions are
descendents of the Exception class). - Catch each type of exception thrown indepently
(definitely preferable)
12Arranging the Catch Clauses
- The Catch clauses should be arranged from most
specific to most general. - If you are going to catch an Exception it should
be the last one in the sequence.
13Example
- try // Some code block catch
(NumberFormatException e) // Handle a
number format catch (IllegalArgumentException
e) // Handle an illegal argument catch
(Exception e) // Handle any exception
14Example (BAD!)
- try // Some code block catch (Exception e)
// This will catch all exceptions the
// following two can never be reached. catch
(IllegalArgumentException e) // Handle an
illegal argument catch (NumberFormatException
e) // Handle a NumberFormat exception
15Dialogs
16Creating (and using) a Dialog Box
- In Swing it is relatively easy to create a number
of dialog boxes. - They are created through a number of classes.
- Can involve input values as well as being
informational.
17JOptionPane.showMessagePane
- Can be used to demonstrate a simple message.
- Does not allow any special input.
- Has the following call pattern
- JOptionPane.showMessageDialog(window,
- "This is an Informational
Message", - "Demo Dialog -- Info
Message", - JOptionPane.INFORMATION_MESS
AGE)
18Dialog Box Appearance
19JOptionPane.showOptionDialog
- Used when a dialog has more than one option (yes,
no, cancel?) - returns an int indicating which button was
pressed. - Can customize the buttons to read whatever you
wish
20Sample Code Dialog
- int resultString buttonLabels new String
"Yes", "Almost"result JOptionPane.showOption
Dialog(window, "Are you ready
for the test Wednesday?", "Question
Dialog", JOptionPane.YES_NO_OPTION, JOptionPan
e.QUESTION_MESSAGE, null, buttonLabels, butt
onLabels1)
21Dialog From Screen
22JOptionPane.showInputDialog
- Used to input a string or one of a number of
choices. - Returns the string.
- Can be closed without returning a string. (the x
in the corner).
23Sample Code Type in Text...
- String s
- s (String) JOptionPane.showInputDialog(window,
- "Input your name ",
- "Input Dialog",
- JOptionPane.PLAIN_MESSAGE,
- null,
- null,
- null)
24Dialog Appearance
25Sample Code Pick From List
- String s
- String options new String "A", "B", "C",
"D", "F" - s (String) JOptionPane.showInputDialog(window,
- "Grade you
expect ", - "Guess Your
Grade", -
JOptionPane.PLAIN_MESSAGE, - null,
- options,
- "A")
26Dialog Appearance
27Adding a File Chooser
- Many applications allow you to have a file
chooser to pick a file rather than typing in a
name. - Simplest form in Java is the JFileChooser
- The value returned is which button was pressed
(OK? or Cancel?)
28Sample Code
- public void showFileChooser( )
- JFileChooser chooser new JFileChooser()
- int option
-
- option chooser.showOpenDialog(window)
- if (option JFileChooser.APPROVE_OPTION)
- System.out.println("You chose "
chooser.getSelectedFile() " from the
list") - else
- System.out.println("You Canceled out of
the Dialog") -
-
-
29The Dialog
30Can Alter What is Selected
- While the example given didn't do it we can set
up a file selection mode that only gives files of
a certain type. - This is all the time I plan on spending on
Dialogs. There are a lot of things that can be
done with them but we have the basics for now