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

Slides:



Advertisements
Similar presentations
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
Advertisements

Chapter 2 - Introduction to Java Applications
Chapter 2: Using Data.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
F27SB2 Software Development 2 Lecture 9: Java GUIs 6.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Java Programming, Second Edition Chapter Five Input and Selection.
CS1101X: Programming Methodology Recitation 3 Control Structures.
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Introduction to GUI in 1 Graphical User Interface Nouf Almunyif.
Object Oriented Programming (OOP) LAB # 3 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
INTERMEDIATE PROGRAMMING USING JAVA
Java Programming Lecture 2
3 Introduction to Classes and Objects.
CS 201 Lecture 7: John Hurley Summer 2012 Cal State LA.
Building a Swing Interface
Data Types – Reference Types
Introduction to Classes and Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Chapter 2 - Introduction to Java Applications
Message, Input, Confirm, and Specialized Dialogs
Introduction to GUI in Graphical User Interface Nouf Almunyif.
Predefined Dialog Boxes
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS18000: Problem Solving and Object-Oriented Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
AP Java 10/4/2016.
Message, Input, and Confirm Dialogs
Object Oriented Programming
CS431 ws99 Half Text Half Graphics
Chapter 2 - Introduction to Java Applications
Object-Oriented Software Engineering
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

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 OK cancel cancel cancel OK error make coupons, show result compute refund, show result error Exit OK OK

Bad Input Dialog Too many dialogs! String red = JOptionPane.showInputDialog( null, "How many red?", "Red Dialog", JOptionPane.QUESTION_MESSAGE ); String blue = JOptionPane.showInputDialog( null, "How many blue?", "Blue Dialog", String green = JOptionPane.showInputDialog( null, "How many green?", "Green Dialog", // 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.