More about JOptionPane Dialog Boxes

Similar documents
Object-Oriented Programming in Java

JOptionPane Dialogs. javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs.

Introduction to Java. Nihar Ranjan Roy.

Course PJL. Arithmetic Operations

Chapter 8: GUI Dialog & Table. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

Chapter 2: Using Data

Chapter 2: Using Data

Guessing Game with Objects

Chapter 02: Using Data

Lesson 3: Accepting User Input and Using Different Methods for Output

ว ฒนพงศ ส ทธภ กด Java Programming ( )

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Guessing Game with Objects

Graphical User Interfaces 2

CT 229 Fundamentals of Java Syntax

Graphical User Interfaces 2

Flow of Control. Chapter 3

Graphical User Interfaces 2

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Example 3-1. Password Validation

Full file at

Introduction to Computers and Engineering Problem Solving 1.00 / Fall 2004

The price of gold. source: nowiknow.com. moodle.yorku.ca CSE 1020

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Flow of Control. Chapter 3. Chapter 3 1

What two elements are usually present for calculating a total of a series of numbers?

In this lab we will practice creating, throwing and handling exceptions.

CSC 111, Test 1 (Fall 2009)

Chapter 3 Selection Statements

Final Examination Semester 3 / Year 2010

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

JDirectoryChooser Documentation

CST141 Thinking in Objects Page 1

What methods does the String class provide for ignoring case sensitive situations?

Building Java Programs Bonus Slides

CompSci 230 S Programming Techniques. Basic GUI Components

CT 229. CT229 Lecture Notes. Labs. Tutorials. Lecture Notes. Programming II CT229. Objectives for CT229. IT Department NUI Galway


Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CS 106 Introduction to Computer Science I

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter 2 Primitive Data Types and Operations

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Control Structures (Deitel chapter 4,5)

Selection as a Control Structure

Chapter 2 ELEMENTARY PROGRAMMING

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Topics. The Development Process

3chapter C ONTROL S TATEMENTS. Objectives

Outline HW2. Feedback. CS1007: Object Oriented Design and Programming in Java

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Understanding the MsgBox command in Visual Basic

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Methods. Eng. Mohammed Abdualal

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

DM503 Programming B. Peter Schneider-Kamp.

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

CS 180. Recitation 8 / {30, 31} / 2007

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Graphical User Interface

Full file at

Midterm Reminders. Design Pattern #6: Facade. General Idea. The Situation

Data Types Reference Types

1.00 Lecture 34. Sorting 2. Reading for next time: Big Java Counting sort

Data Structures and Algorithms

MIT AITI Swing Event Model Lecture 17

TTh 9.25 AM AM Strain 322

Chapter 1 Introduction to Computers, Programs, and Java

Part 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented

Introduction to Scientific Programming Using Java

Selection and Repetition Revisited

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Date: Dr. Essam Halim

THE UNIVERSITY OF AUCKLAND

4 WORKING WITH DATA TYPES AND OPERATIONS

Introduction to Classes and Objects

1.00 Lecture 3. Main()

Chapter 2 Elementary Programming

Version 8.0 PREEMPTIVE SOLUTIONS DASHO. User Guide

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Use of objects and variables

Addendum File Web Design and Development (CS506)

Computer Programming I - Unit 5 Lecture page 1 of 14

Transcription:

APPENDIX K More about JOptionPane Dialog Boxes In Chapter 2 you learned how to use the JOptionPane class to display message dialog boxes and input dialog boxes. This appendix provides a more detailed discussion of the dialog boxes you can create using JOptionPane. We will discuss the following types of dialog boxes and how you can display them. Message Dialog. This is a dialog box that displays a message. An OK button is also displayed. Input Dialog. This is a dialog box that prompts the user for input. It provides a text field where input is typed. An OK button and a Cancel button are also displayed. Confirm Dialog. This is a dialog box that asks the user a Yes/No question. A Yes button, a No button, and a Cancel button are displayed. Figure K-1 shows an example of each type of dialog box. Figure K-1 Message Box, Input Box, and Confirm Box K-1

K-2 Appendix K More about JOptionPane Dialog Boxes The JOptionPane class, which is in the javax.swing package, provides static methods to display each type of dialog box. More about Message Dialogs The showmessagedialog method is used to display a message dialog. There are several overloaded versions of this method. Table K-1 describes two of the versions. Table K-1 The showmessagedialog Method Method void showmessagedialog (Component parent, Object message) void showmessagedialog (Component parent, Object message, String title, int messagetype) Descriptions This method displays a message dialog. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. This method displays a message dialog. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. The string passed to the title parameter is displayed in the dialog box s title bar. The value passed to messagetype indicates the type of icon to display in the message box. Here is a statement that calls the first version of the method: JOptionPane.showMessageDialog(null, "Hello World"); Figure K-2 Message dialog box The first argument can be a reference to a graphical component. The dialog box is displayed inside that component. In this statement we pass null as the first argument. This causes the dialog box to be displayed in the center of the screen. The second argument is the message that we wish to display. This code causes the dialog box in Figure K-2 to appear.

More about Message Dialogs K-3 Notice that by default the dialog box in Figure K-2 has the string "Message" displayed in its title bar, and an information icon (showing the letter i ) is displayed. You can control the text that is displayed in the title bar and the type of icon that is displayed with the second version of the showmessagedialog method. Here is an example: JOptionPane.showMessageDialog(null, "Invalid Data", "My Message Box", JOptionPane.ERROR_MESSAGE); In this method call, the third argument is a string that is displayed in the dialog box s title bar. The fourth argument is a constant that specifies the type of message that is being displayed, which determines the type of icon that appears in the dialog box. The constant JOptionPane.ERROR_MESSAGE specifies that an error icon is to be displayed. This statement displays the dialog box shown in Figure K-3. Figure K-3 Message dialog with specified title and icon The constants that you may use for the message type are JOptionPane.ERROR_MESSAGE, JOptionPane. INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane. QUESTION_MESSAGE, and JOptionPane.PLAIN_MESSAGE. The following statements call the method with each type of message. Figure K-4 shows the dialog boxes displayed by these messages. // Display an error message. JOptionPane.showMessageDialog(null, "Error Message", "Error", JOptionPane.ERROR_MESSAGE); // Display an information message. JOptionPane.showMessageDialog(null, "Information Message", "Information", JOptionPane.INFORMATION_MESSAGE); // Display a warning message. JOptionPane.showMessageDialog(null, "Warning Message", "Warning", JOptionPane.WARNING_MESSAGE); // Display a question message. JOptionPane.showMessageDialog(null, "Question Message", "Question", JOptionPane.QUESTION_MESSAGE);

K-4 Appendix K More about JOptionPane Dialog Boxes // Display a plain message. JOptionPane.showMessageDialog(null, "Plain Message", "Message", JOptionPane.PLAIN_MESSAGE); Figure K-4 Different types of messages If the previous code were written into a program just as it appears and then executed, the five dialog boxes shown in Figure K-4 would be displayed one at a time. The user would have to click the OK button on the first dialog box to close it before the second dialog box would appear. The same would be true for all of the dialog boxes that follow. The dialog boxes displayed by the JOptionPane class are modal dialog boxes. A modal dialog box suspends execution of any other statements until the dialog box is closed. For example, when the JOptionPane.showMessageDialog method is called, the statements that appear after the method call do not execute until the user closes the message box. This is illustrated in Figure K-5. Figure K-5 Execution of statement after displaying a modal dialog box

More about Input Dialogs An input dialog is a quick and simple way to ask the user to enter data. Table K-2 describes two overloaded versions of the static showinputdialog method, which displays an input dialog. The following code calls the first version of the showinputdialog method: String name; name = JOptionPane.showInputDialog("Enter your name."); More about Input Dialogs K-5 Table K-2 The showinputdialog Method Method String showinputdialog (Object message) String showinputdialog (Component parent, Object message, String title, int messagetype) Description This method displays an input dialog that provides a text field for the user to type input. The object passed to the message parameter contains the message that is to be displayed. If the user clicks on the OK button, this method returns the string that was entered by the user. If the user clicks on the Cancel button, this method returns null. This method displays an input dialog that provides a text input field for the user to type input. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. The string passed to the title parameter is displayed in the dialog box s title bar. The value passed to messagetype indicates the type of icon to display in the message box. If the user clicks on the OK button, this method returns the string that was entered by the user. If the user clicks on the Cancel button, this method returns null. The argument passed to the method is the message to display. This statement causes the dialog box shown in Figure K-6 to be displayed in the center of the screen. If the user clicks on the OK button, name references the string value entered by the user into the text field. If the user clicks the Cancel button, name references null. Figure K-6 Input dialog box

K-6 Appendix K More about JOptionPane Dialog Boxes By default the input dialog box has the string Input in its title bar and displays a question icon. The second version of the method shown in Table K-2 allows you to control the text displayed in the input dialog s title bar and the type of icon displayed. It takes the same arguments as the second version of the showmessagedialog method in Table K-1. Here is an example: String value; value = JOptionPane.showInputDialog(null, "Enter the value again.", "Enter Carefully!", JOptionPane.WARNING_MESSAGE); This statement displays the input dialog shown in Figure K-7. If the user clicks on the OK button, value references the string value entered by the user into the text field. If the user clicks on the Cancel button, value references null. Figure K-7 Input dialog box Displaying Confirm Dialogs A confirm dialog box typically asks the user a Yes or No question. By default a Yes button, a No button, and a Cancel button are displayed. The showconfirmdialog method is used to display a confirm dialog box. There are several overloaded versions of this method. Table K-3 describes two of them. The following code calls the first version of the method: int value; value = JOptionPane.showConfirmDialog(null, "Are you sure?"); The first argument can be a reference to a graphical component, and the dialog box is displayed inside that component. In this statement we pass null, which causes the dialog box to be displayed in the center of the screen. The second argument is the message that we wish to display. This code causes the dialog box in Figure K-8 to appear. By default the confirm dialog box displays Select an Option in its title bar, a Yes button, a No button, and a Cancel button. The showconfirmdialog method returns an integer that represents the button clicked by the user. You can determine which button the user clicked by comparing the method s return value to one of the following constants: JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION.

Displaying Confirm Dialogs K-7 Table K-3 The showconfirmdialog Method Method int showconfirmdialog (Component parent, Objectƒmessage) int showconfirmdialog (Component parent, Object message, String title, int optiontype) Description The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. The method returns an integer that represents the button clicked by the user. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. The string passed to the title parameter is displayed in the dialog box s title bar. The value passed to optiontype indicates the types of buttons to display in the dialog box. The method returns an integer that represents the button clicked by the user. Figure K-8 Confirm dialog box Here is an example: int value; value = JOptionPane.showConfirmDialog(null, "Are you sure?"); if (value == JOptionPane.YES_OPTION) { If the user clicked Yes, the code here is executed. } else if (value == JOptionPane.NO_OPTION) { If the user clicked No, the code here is executed. } else if (value == JOptionPane.CANCEL_OPTION) { If the user clicked Cancel, the code here is executed. }

K-8 Appendix K More about JOptionPane Dialog Boxes The second version of the method shown in Table K-3 allows you to control the text displayed in the confirm dialog s title bar and the type of buttons that are displayed. The first three arguments are the-same as those used for the second version of the showmessagedialog method in Table K-1. The fourth argument specifies the types of buttons that are to appear in the dialog box. You may use one of the following constants: JOptionPane.YES_ NO_OPTION or JOptionPane.YES_NO_CANCEL_OPTION. For example, the following code displays a confirm dialog box with only a Yes button and a No button, as shown in Figure K-9. int value; value = JOptionPane.showConfirmDialog(null, "Are you sure?", "Please Confirm", JOptionPane.YES_NO_OPTION); Figure K-9 Confirm dialog box with a Yes button and a No button An Example Program The program in Code Listing K-1 displays each of the types of dialog boxes we have discussed. Code Listing K-1 (TestAverageDialog.java) 1 /** 2 This program demonstrates different types of 3 dialog boxes. 4 */ 5 6 import javax.swing.joptionpane; 7 8 public class TestAverageDialog 9 { 10 public static void main(string [] args) 11 { 12 String input; // User input 13 int score1, score2, score3; // test scores 14 double average; // Average test score 15 int repeat; // Confirm dialog button clicked 16 17 do 18 {

An Example Program K-9 19 // Get the three test scores. 20 input = JOptionPane.showInputDialog(null, 21 "Enter score #1."); 22 score1 = Integer.parseInt(input); 23 24 input = JOptionPane.showInputDialog(null, 25 "Enter score #2."); 26 score2 = Integer.parseInt(input); 27 28 input = JOptionPane.showInputDialog(null, 29 "Enter score #3."); 30 score3 = Integer.parseInt(input); 31 32 // Calculate and display the average test score. 33 average = (score1 + score2 + score3) / 3.0; 34 JOptionPane.showMessageDialog(null, 35 "The average is " + average); 36 37 // Does the user want to average another set? 38 repeat = JOptionPane.showConfirmDialog(null, 39 "Would you like to average another " + 40 "set of test scores?", "Please Confirm", 41 JOptionPane.YES_NO_OPTION); 42 43 } while (repeat == JOptionPane.YES_OPTION); 44 45 System.exit(0); 46 } 47 } When this program executes, the dialog boxes shown in Figure K-10 are displayed, one at a time. Notice the last statement in this program, in line 45: System.exit(0); This statement causes the program to end and is required in any GUI program. Unlike a console program, a GUI program does not automatically stop executing when the end of the main method is reached. This is because Swing generates a thread, which is a process running in the computer. If the System.exit method is not called, this thread continues to execute, even after the end of the main method has been reached. The System.exit method requires an integer argument. This argument is an exit code that is passed back to the operating system. Although this code is usually ignored, it can be used outside the program to indicate whether the program ended successfully or as the result of a failure. The value 0 traditionally indicates that the program ended successfully.

K-10 Appendix K More about JOptionPane Dialog Boxes Figure K-10 Dialog boxes displayed by the TestAverageDialog program The first dialog box appears as shown here with example input. When the user clicks OK, the second dialog box appears, as shown here with example input. When the user clicks OK, the third dialog box appears, as shown here with example input. When the user clicks OK, the fourth dialog box appears, as shown here. When the user clicks OK, the fifth dialog box appears, as shown here. If the user clicks Yes, the loop will iterate again.