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

Similar documents
More about JOptionPane Dialog Boxes

Object-Oriented Programming in Java

Course PJL. Arithmetic Operations

Guessing Game with Objects

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

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

Introduction to Java. Nihar Ranjan Roy.

Guessing Game with Objects

CST141 Thinking in Objects Page 1

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

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

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

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I

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.

Chapter 2: Using Data

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

CS 106 Introduction to Computer Science I

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

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

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

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

Addendum File Web Design and Development (CS506)

Full file at

Selection and Repetition Revisited

Introduction to Classes and Objects

CSC 111, Test 1 (Fall 2009)

Chapter 2: Using Data

CT 229 Fundamentals of Java Syntax

SCOPE. public class ABC { public static void main( String args [ ] ) { Not accessible outside the block. Local to this block; accessible within it

Chapter 02: Using Data

Introduction to Programming Using Java (98-388)

Recitation 3 Class and Objects

Homework Set 2- Class Design

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

COMP 102: Test. 2017, May 15 ** WITH SOLUTIONS **

Selection and Repetition

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

Data Types Reference Types

THE UNIVERSITY OF AUCKLAND

JDirectoryChooser Documentation

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

Introduction to Computers and Engineering Problem Solving 1.00 / Fall 2004

Model Solutions. COMP 102: Test May, 2015

Flow of Control. Chapter 3

CSC 1051 Data Structures and Algorithms I

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

CSC 1051 Data Structures and Algorithms I

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Control Structures (Deitel chapter 4,5)

Flow of Control. Chapter 3. Chapter 3 1

Chapter 4 Introduction to Control Statements

Common Misunderstandings from Exam 1 Material

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Chapter 3 - Introduction to Java Applets

Java Coding 3. Over & over again!

Java Programming Lecture 6


BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

Topics. The Development Process

Chapter 2 ELEMENTARY PROGRAMMING

Data Structures and Algorithms

CSC 1051 Data Structures and Algorithms I

Control Statements: Part Pearson Education, Inc. All rights reserved.

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

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

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

COMP 102: Test , September 25 ** WITH SOLUTIONS **

CS111: PROGRAMMING LANGUAGE II

Programming for Mobile Computing

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!

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Week 9 Implementation

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

COMP 202 Java in one week

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

COMP 202. Java in one week

Chapter 4 Control Structures: Part 2

Array. Prepared By - Rifat Shahriyar

TTh 9.25 AM AM Strain 322

An Exceptional Class. by Peter Lavin. June 1, 2004

Introduction to Computer Science I

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Programming: Test 2

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

Introduction to Scientific Programming Using Java

Graphical User Interface

Generating/Updating code from whole project

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

The static Keyword. Lecture 2 Java Intermediate. The static Keyword. The Plan. Class Attributes The static Keyword. Class Methods The static Keyword

Fundamentals of Programming Data Types & Methods

Introduction to the JAVA UI classes Advanced HCI IAT351

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

א א א E2-1F Welcome to Java Programming!

Model Solutions. COMP 102: Test May, 2016

CT 229 Java Syntax Continued

Chapter 2: Java Fundamentals

Transcription:

JOptionPane Dialogs javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create 4 Common Dialogs: Message Dialog - display a message Confirm Dialog - Yes, No, or Cancel dialog Choice Dialog - click a button to make choice Input Dialog - request input, return a string

Dialog Examples JOptionPane is a class for showing dialog boxes. It can display dialogs like these: showmessagedialog(...) showconfirmdialog(...) reply = showinputdialog(...)

Message Dialog // basic message dialog JOptionPane.showMessageDialog( null, "Wake Up!"); // message dialog with a title and message type. JOptionPane.showMessageDialog( null, "Wake Up!", "An Information Message", JOptionPane.INFORMATION_MESSAGE ); Syntax: static void showmessagedialog( Component parent, Object message ) static void showmessagedialog( Component parent, Object message, String title_line, int messagetype ) messagetype is one of: INFORMATION_MESSAGE, QUESTION_MESSAGE, WARNING_MESSAGE, ERROR_MESSAGE

Modal or Non-Modal Dialog? Graphics frameworks support 2 ways of using dialogs: Modal Dialog - user must close the dialog (click OK, Cancel, etc) before he can work in the parent window. Example: "Open file" dialog. Non-modal Dialog - user can go back and work in the parent window while the dialog is still open. JOptionPane.showMessageDialog( parent, "Wake Up!" ); First parameter is a reference to parent window. if parent == null, it displays a non-modal dialog. if parent!= null, it displays a modal dialog

Multi-line Message Dialog String message = "Please give the customer:\n" + "2 Rice balls\n" + "1 Green tea\n" + "5 Tofu sticks"); JOptionPane.showMessageDialog( null, message, "Order Details", JOptionPane.INFORMATION_MESSAGE ); Use correct singular / plural Avoid errors like this: You have 1 new messages

Confirm Dialog int choice; choice = JOptionPane.showConfirmDialog( null, "Are you awake?", "This is a Confirm Dialog", JOptionPane.YES_NO_CANCEL_OPTION ); if ( choice == JOptionPane.NO_OPTION ) JOptionPane.showMessageDialog( null, "Liar!" ); Syntax: static int showconfirmdialog( Component parent, Object message, String title_line, int optiontype ) optiontype is: YES_NO_OPTION or YES_NO_CANCEL_OPTION return value is: YES_OPTION, NO_OPTION, or CANCEL_OPTION

Input Dialog String reply; reply = JOptionPane.showInputDialog( null, "What do you want?", "This is an Input Dialog", JOptionPane.QUESTION_MESSAGE ); if ( reply == null ) /* user pressed CANCEL */ ; else dosomething( reply ); Syntax: static String showinputdialog( Component parent, Object message, String title_line, int messagetype ) return value is null if "Cancel" pressed; else string typed in textbox.

Option Dialog String [] choices = { "Buy Coupons", "Refund", "Kill Customer", "Quit"}; int reply = JOptionPane.showOptionDialog( null, // parent "Choose Action:", // message "This is an Options Dialog", // title string JOptionPane.YES_NO_OPTION, // useless JOptionPane.QUESTION_MESSAGE, // msg type null, // no icon choices, // array of choices choices[0] // default choice ); switch( reply ) { case 0: coupondialog( ) ; break; case 1: refunddialog( ) ; break; case 2: killcustomer( ) ; break; default: confirmquit( ) ; }

JOptionPane Usage These 4 dialogs are static methods: you don't need to create an instance of JOptionPane. JOptionPane also has instance methods for more control over dialogs. See the Java API. JOptionPane is in javax.swing: import javax.swing.joptionpane; or: import javax.swing.*;

KU Coupons Design using Dialogs Draw a UML state chart diagram. Start Input wait state (display choice dialog) "Buy" action "Quit" action "Refund" action Buy Coupons dialog Confirm quit dialog Refund dialog OK cancel cancel OK cancel OK error make coupons, show result Exit compute refund, show result error OK OK

Bad Input Dialog String red = JOptionPane.showInputDialog( null, "How many red?", "Red Dialog", JOptionPane.QUESTION_MESSAGE ); String blue = JOptionPane.showInputDialog( null, "How many blue?", "Blue Dialog", JOptionPane.QUESTION_MESSAGE ); String green = JOptionPane.showInputDialog( null, "How many green?", "Green Dialog", JOptionPane.QUESTION_MESSAGE ); // now parse the Strings Too many dialogs! No feedback from previous dialog (how many red and blue did he enter?).

Better Input Dialog Display input data for verification and feedback.

Hints How to convert a String containing a number like "123" to a number? Use results of Homework 2, or use Scanner. How to process a String containing several numbers, such as "12 25 7.5 4"? Look at the API for the "Scanner" class. There is a constructor that accepts a String argument Scanner( String source ); String reply = /* string containing numbers */ ; Scanner scan = new Scanner( reply ); /* now use Scanner methods to read the values */

Crashable Input Method private void withdrawdialog( ) { String reply = JOptionPane.showInputDialog( null, "Please input amount to withdraw", "Withdraw Calculator", JOptionPane.QUESTION_MESSAGE ); Scanner scan = new Scanner( reply ); // if user presses CANCEL this will crash! amount = scan.nextdouble( ); // crash! if no nextdouble // what if user inputs TOO MUCH data?

Writing a Crash-proof Method private void withdrawdialog( ) { String reply = JOptionPane.showInputDialog( null,..., // same as previous slide JOptionPane.QUESTION_MESSAGE ); if ( reply == null ) return; // dialog cancelled Scanner scan = new Scanner( reply ); boolean inputok = true; // test before reading if ( scan.hasnextdouble() ) red = scan.nextint( ); else inputok = false; // test for too much data inputok = inputok && (! scan.hasnext() ); if ( inputok ) { /* compute withdraw */ } else { /* display error message */ }

Making Your Program Crash-proof Don't assume input exists or is of expected type. Ask someone else to test your program! Tester should be malicious.