Miscellaneous Topics Announcements, Exceptions, and Dialogs - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Miscellaneous Topics Announcements, Exceptions, and Dialogs

Description:

Are you able to write a list of your items out to a file? Can you read them back in? This week's lab deals a bit with GUI's ... Stores information on an album ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 31
Provided by: geraldr6
Category:

less

Transcript and Presenter's Notes

Title: Miscellaneous Topics Announcements, Exceptions, and Dialogs


1
Miscellaneous TopicsAnnouncements, Exceptions,
and Dialogs
  • November 14, 2005

2
Test Wednesday
  • The second test is Wednesday!!
  • Closed book and notes.
  • No calculators.
  • 5-6 Questions.
  • Includes Applets, GUIs, Loops, and Arrays.

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

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

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

6
Exceptions
  • 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.

7
Try, 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

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

9
There Are All Kinds Of Exceptions
  • Exception
  • RuntimeException
  • ArithmeticException
  • ArrayStoreException
  • ClassCastException
  • IllegalArgumentException
  • NumberFormatException
  • IllegalMonitorStateException
  • IndexOutOfBoundsException
  • ArrayIndexOutOfBoundsException
  • StringIndexOutOfBoundsException
  • NegativeArraySizeException
  • NullPointerException
  • SecurityException

10
Making 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!)

11
Catching 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)

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

13
Example
  • try // Some code block catch
    (NumberFormatException e) // Handle a
    number format catch (IllegalArgumentException
    e) // Handle an illegal argument catch
    (Exception e) // Handle any exception

14
Example (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

15
Dialogs
16
Creating (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.

17
JOptionPane.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)

18
Dialog Box Appearance
19
JOptionPane.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

20
Sample 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)

21
Dialog From Screen
22
JOptionPane.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).

23
Sample Code Type in Text...
  • String s
  • s (String) JOptionPane.showInputDialog(window,
  • "Input your name ",
  • "Input Dialog",
  • JOptionPane.PLAIN_MESSAGE,
  • null,
  • null,
  • null)

24
Dialog Appearance
25
Sample 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")

26
Dialog Appearance
27
Adding 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?)

28
Sample 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")

29
The Dialog
30
Can 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
Write a Comment
User Comments (0)
About PowerShow.com