The Useful Joptionpane Class

The Useful Joptionpane Class

<p> The Useful JOptionPane Class</p><p>Note: the following link will take you to the horse’s mouth (Sun) to get full detailed information about the JOptionPane class and the nice features it offers: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JOptionPane.html A summary of some of those nice features follows. For many methods more than one version may exist, but only a few are documented here.</p><p>Method 1: showInputDialog (this method returns an object reference to a String) – 6 versions</p><p>Version 1: give it actual parameters of an Object (actually a Sting in most cases). This argument is the message printed in the window to prompt the user for input. This uses the question mark icon in the window. Sample: userInput = JOptionPane.showInputDialog(“Your age?”);</p><p>Version 2: this version uses 4 arguments. The first is an object reference to the parent container (form many simple uses this is just set to null). The second argument is the String to be used as the prompt for the input. The third argument is the String to be used as the title for the window and the last argument is an int that represents the type of the message. This last argument is usually one of the class constants defined for this purpose by the JOptionPane class. These include: JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE , JOptionPane.WARNING_MESSAGE, JOptionPane.QUESTION_MESSAGE and JOptionPane.PLAIN_MESSAGE . This last choice influences which icon is shown in the window. Sample: userInput = JOptionPane.showInputDialog(null,“Your age?”,”Input”, JOptionPane.INFORMATION_MESSAGE);</p><p>Fact: if the user click on the cancel button (or X in upper right) the String reference returned is null! If the user hits the enter key without typing anything, an object reference to the null String (i.e., “”) is returned. These are not the same outcome. You MUST verify that the String reference is non-null before you attempt to use it in ANY fashion or you get a RTE message.</p><p>Method 2: showMessageDialog (these are void class methods) – 3 versions</p><p>Version 1: give it 2 actual parameters. The first argument is a reference the parent container (usually null in simple cases). The second argument is an object that is to be displayed. This is often a String but can be other things! The information-icon is used as no message type is specified. Sample: JOptionPane.showMessageDialog(null, myStringMessage);</p><p>Version 2: this version uses 4 arguments. The first is an object reference to the parent container (form many simple uses this is just set to null). The second argument is an Object that is to be displayed (often a String) acting as a prompt for the input. The third argument is the String to be used as the title for the window and the last argument is an int that represents the type of the message. See Version 2 above to see common class constants used for this last argument. This last choice influences which icon is shown in the window. Sample: JOptionPane.showMessageDialog(null,myStringMessage,”Window Title”, JOptionPane.ERROR_MESSAGE); Sample 2: This second example creates a JTextArea (of size 5 by 15) and then populates the text area with 11 lines of text (only 5 will show at a time). This text area is then used to make a scrollable pane that will allow end user to scroll up and down to see 11 lines. The horizontal scrollbar will only be displayed if lines longer than 15 were added to text area.</p><p>JTextArea myTextArea = new JTextArea(5,15); // rows and columns myTextArea.setEditable(false); // Cannot edit text area contents. JScrollPane scrollPane = new JScrollPane(myTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // Always vertical scrolling JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // maybe horizontal myTextArea.setText( “Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n” + “Line 6\nLine 7\nLine 8\nLine 9\nLine 10”); myTextArea.append(“\nBingo”); // To add a line to existing 10 lines JOptionPane.showMessageDialog(null,scrollPane);</p><p>Method 3: showConfirmDialog (these are int-returning class methods) – 4 versions</p><p>Version 1: give it 2 actual parameters. The first argument is a reference the parent container (usually null in simple cases). The second argument is the Message to be displayed. This dialog will offer a Yes a No and a Cancel button. The int returned (by its value) tells you which the user clicked on. This class defines several int-class constants to represent the possible values of the returned int value. Sample: int answer = JOptionPane.showConfirmDialog(null, myStringQuestion); if (answer == JOptionPane.YES_OPTION) // Do yes Stuff else if (answer == JOptionPane.NO_OPTION) // Do no stuff else if (answer == JOptionPane.CANCEL_OPTION) // Do cancel stuff else if (answer == JOptionPane.CLOSED_OPTION) // He closed the window! else // Should never reach here!</p><p>Possible return values can be represented by: YES_OPTION, NO_OPTION , CANCEL_OPTION, OK_OPTION and CLOSED_OPTION .</p><p>Version 2: this version uses 4 arguments. The first is an object reference to the parent container (form many simple uses this is just set to null). The second argument is the String to be used as the prompt for the input. The third argument is the String to be used as the title for the window and the last argument is an int that represents the collection of buttons to be offered on the bottom. The JOptionPane class offers several class int constants that can be used to describe the desired buttons. DEFAULT_OPTION (OK only), YES_NO_OPTION, YES_NO_CANCEL_OPTION , OK_CANCEL_OPTION </p><p>Sample: int answer = JOptionPane.showConfirmDialog(null, myStringQuestion, “Window Title”, JOptionPane.YES_NO_OPTION); // The above offers only the Yes and No buttons if (answer == JOptionPane.YES_OPTION) // Do yes Stuff else if (answer == JOptionPane.NO_OPTION) // Do no stuff else if (answer == JOptionPane.CLOSED_OPTION) // He closed the window! else // Should never reach here!</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us