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.

Slides:



Advertisements
Similar presentations
Components and Containers
Advertisements

Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
Chapter 2 - Introduction to Java Applications
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.
Object-Oriented Programming with Java Java with added Swing Lecture 3.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
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.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
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 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
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 development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Graphical User Interfaces. Graphical input and output with JOptionPane.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
Basics of GUI Programming Chapter 11 and Chapter 22.
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.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Topics Graphical User Interfaces Using the tkinter Module
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
“Form Ever Follows Function” Louis Henri Sullivan
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Message, Input, Confirm, and Specialized Dialogs
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
Timer class and inner classes
Predefined Dialog Boxes
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS18000: Problem Solving and Object-Oriented Programming
Chapter 2 - Introduction to Java Applications
Message, Input, and Confirm Dialogs
Object Oriented Programming
CS431 ws99 Half Text Half Graphics
JOptionPane class.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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 of type JDialog Object Component Container Window Dialog JDialog

Using JDialog ● JDialog, like JFrame, JWindow, and JApplet, uses a content pane to organize its contents ● If you create a JDialog directly, you are responsible for: – Laying out the dialog components ( BorderLayout by default): ● Messages and Icons ● Input areas and Buttons (Yes, No, Ok, Cancel, etc.) – Adding listeners to the dialog components – Showing and hiding the dialog when appropriate

Another Way: JOptionPane ● The JOptionPane class has a number of static methods that do the work of creating, laying out, and adding listeners for dialogs that perform standard functions ● The methods are of the form showXXXDialog where XXX is: – Message : Tell the user about something that has happened – Confirm : Ask a confirming question, like yes/no/cancel – Input : Prompt for some input – Option : Combination of the above

General Dialog Layout Icon Message Input Value Option Buttons ● Note: the dialogs created by the JOptionPane class are modal, that is, processing is blocked until user interaction is complete

Arguments Accepted by showXXXDialog Methods ● Parent component: used to position dialog; if null then dialog is centered on screen ● Message: a descriptive message to be displayed in the dialog box, often a String ● Message type: coded integer specifying the style of the message: – JOptionPane.ERROR_MESSAGE – JOptionPane.INFORMATION_MESSAGE – JOptionPane.QUESTION_MESSAGE – JOptionPane.WARNING_MESSAGE – JOptionPane.PLAIN_MESSAGE

Arguments Accepted by showXXXDialog Methods (cont'd) ● Title: title to appear on dialog's title bar ● Button options: coded integer defining the set of available response buttons: – JOptionPane.OK_CANCEL_OPTION – JOptionPane.YES_NO_OPTION – JOptionPane.YES_NO_CANCEL_OPTION – JOptionPane.DEFAULT_OPTION ● Icon: icon to be displayed by message. If null, a default is used depending on Message type and Look-and-Feel

Arguments Accepted by showXXXDialog Methods (cont'd) ● Selection values: an array of Object s, often String s, to be selected from when getting input from user ● Initial selection value: value from Selection values to be selected by default

An Example Dialog Task ● Put up a dialog that: – Displays an icon indicating this is a dialog that is transmitting information – Displays the message ''A CATASTROPHIC ERROR HAS OCCURRED'' – Displays a single button labeled ''OK'' – Adds a listener to the button that causes the dialog to disappear when the button is clicked, allowing processing to continue

A Simple Message Dialog import javax.swing.*; public class DialogTest { public static void main(String[] args) { JOptionPane.showMessageDialog( null, "A CATASTROPHIC ERROR HAS OCCURRED."); System.exit(0); }

Notes On The Example ● As a static method, showMessageDialog must be fully qualified by the class name ● As a void method, showMessageDialog is called for side effect ● The first argument is the dialog's parent component. If null, the dialog is centered on the screen ● The second argument is an Object to be displayed, often a String ● Output:

Dialogs That Ask Questions ● The showConfirmDialog method can present more than one button, from among Yes, No, OK, and Cancel ● A symbolic integer is returned indicating which button was clicked : – JOptionPane.CANCEL_OPTION – JOptionPane.YES_OPTION – JOptionPane.NO_OPTION – JOptionPane.OK_OPTION ● Use this value to determine the course of action to take after interaction with the user

showConfirmDialog Example... int response = JOptionPane.showConfirmDialog( null, "DO YOU WANT TO CONTINUE?"); if (response == JOptionPane.YES_OPTION) {... // user clicked YES } else if (response == JOptionPane.NO_OPTION) {... // user clicked NO } else {... // user clicked CANCEL }...

Changing the Dialog Title and Buttons ● All showXXXDialog methods have multiple versions allowing different combinations of arguments ● Suppose you want only the OK and Cancel buttons, and a non-default dialog title:

Changing the Dialog Title and Buttons (cont'd)... int response = JOptionPane.showConfirmDialog( null, "DO YOU WANT TO CONTINUE?", "Continue Dialog", // Dialog title JOptionPane.OK_CANCEL_OPTION); if (response == JOptionPane.OK_OPTION) {... // user clicked OK } else {... // user clicked CANCEL }

Getting Textual Input ● showInputDialog can return a string that is input by the user ● There are many argument combinations. Here is the simplest:... String ssn = JOptionPane.showInputDialog( "ENTER YOUR SOCIAL SECURITY NUMBER");...

Getting List Selections Suppose you want a dialog to present the user with a list of possible selections: When the user clicks on the input area, a drop-down list appears:

Getting List Selections (cont'd) This requires a 7-argument call to showInputDialog :... String message = "Select your favorite cereal:"; String title = "Cereal Selection"; Object[] selectionValues = {"Corn Flakes", "Wheaties", "Cheerios"}; Object response = JOptionPane.showInputDialog( null, message, title, JOptionPane.QUESTION_MESSAGE, // message type null, // icon selectionValues, // selections selectionValues[0]); // initially // selected String favoriteCereal = (String)response;...