Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Components and Containers
Advertisements

Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
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.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
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:
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
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.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
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.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
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.
Classes 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.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
제 14 장 고급 스윙 컴포넌트.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©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
Lecture 15 Basic GUI programming
Building a Swing Interface
2.5 Another Java Application: Adding Integers
CSE 114 – Computer Science I Event Programming
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.
CS18000: Problem Solving and Object-Oriented Programming
Message, Input, and Confirm Dialogs
Advanced Programming in Java
Object Oriented Programming
JOptionPane class.
Presentation transcript:

Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/30 ▪O▪Option Panes ▪P▪Program

Option Panes ▪Option panes are popup dialog boxes ▪There are four types of option panes in Java ›Message dialog – shows a message and waits for the user to click OK ›Confirmation dialog – shows a question and asks for confirmation, such as OK or Cancel ›Input dialog – shows a question and gets the user’s input from a text field, combo box, or list ›Option dialog – shows a question and gets the user’s answer from a set of options ▪To display an option pane, there are static methods on the JOptionPane class called showMessageDialog, showConfirmationDialog, showInputDialog, and showOptionDialog USC CSCI 201L3/30 Option Panes

Message Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.ImageIcon; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JOptionPane; 8 9 public class Test extends JFrame { 10 public Test() { 11 super("JOptionPane Example"); 12 JButton jb = new JButton("Click Here"); 13 jb.addActionListener(new ActionListener() { 14 public void actionPerformed(ActionEvent ae) { 15 JOptionPane.showMessageDialog(Test.this, 16 “Message", 17 “Message Dialog", 18 JOptionPane.INFORMATION_MESSAGE, 19 new ImageIcon("uscshield.png")); 20 } 21 }); 22 add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 25 setSize(250, 225); 26 setLocationRelativeTo(null); 27 setVisible(true); 28 } public static void main(String args[]) { 31 Test t = new Test(); 32 } 33 } USC CSCI 201L4/30 Option Panes

Message Dialog Types JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.WARNING_MESSAGE); USC CSCI 201L5/30 Option Panes JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.QUESTION_MESSAGE);

Confirmation Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 import javax.swing.JButton; 4 import javax.swing.JFrame; 5 import javax.swing.JOptionPane; 6 7 public class Test extends JFrame { 8 public Test() { 9 super("JOptionPane Example"); 10 JButton jb = new JButton("Click Here"); 11 jb.addActionListener(new ActionListener() { 12 public void actionPerformed(ActionEvent ae) { 13 int selection = JOptionPane.showConfirmDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_OPTION); 14 switch (selection) { 15 case JOptionPane.YES_OPTION: // case JOptionPane.OK_OPTION is the same 16 System.out.println("Yes"); 17 break; 18 case JOptionPane.NO_OPTION: 19 System.out.println("No"); 20 break; 21 case JOptionPane.CANCEL_OPTION: 22 System.out.println("Cancel"); 23 break; 24 case JOptionPane.CLOSED_OPTION: 25 System.out.println("Closed"); 26 break; 27 } 28 } 29 }); 30 add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 setSize(250, 225); 34 setLocationRelativeTo(null); 35 setVisible(true); 36 } public static void main(String args[]) { 39 Test t = new Test(); 40 } 41 } USC CSCI 201L6/30 Option Panes

Confirmation Dialog Types JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_OPTION); JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION); JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.OK_CANCEL_OPTION); USC CSCI 201L7/30 Option Panes

Input Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JButton; 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 8 public class Test extends JFrame { 9 public Test() { 10 super("JOptionPane Example"); 11 JButton jb = new JButton("Click Here"); 12 jb.addActionListener(new ActionListener() { 13 public void actionPerformed(ActionEvent ae) { 14 String value = JOptionPane.showInputDialog(Test.this, 15 "Input", 16 "Input Dialog", 17 JOptionPane.QUESTION_MESSAGE); 18 System.out.println("value = " + value); 19 if (value == null) { 20 System.out.println("Cancel button or X clicked"); 21 } 22 } 23 }); add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 setSize(250, 225); 29 setLocationRelativeTo(null); 30 setVisible(true); 31 } public static void main(String args[]) { 34 Test t = new Test(); 35 } 36 } USC CSCI 201L8/30 Option Panes

Input Dialog Types String value = JOptionPane.showInputDialog(Test.this, “Input", “Input Dialog", JOptionPane.QUESTION_MESSAGE); Object values[] = {"option 1", "option 2", "option 3"}; Object defaultValue = "option 2"; Object value = JOptionPane.showInputDialog(Test.this, "Input", "Input Dialog", JOptionPane.QUESTION_MESSAGE, null, // icon values, defaultValue); USC CSCI 201L9/30 Option Panes

Input Dialog Types (cont.) ▪If you have more 20 or more items in the list, you will have a multiple selection box instead of a drop down list Object values[] = new Object[20]; for (int i=0; i < 20; i++) { values[i] = “option “ + (i+1); } Object defaultValue = "option 2"; Object value = JOptionPane.showInputDialog(Test.this, "Input", "Input Dialog", JOptionPane.QUESTION_MESSAGE, null, // icon values, defaultValue); USC CSCI 201L10/30 Option Panes

Option Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JButton; 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 8 public class Test extends JFrame { 9 public Test() { 10 super("JOptionPane Example"); 11 JButton jb = new JButton("Click Here"); 12 jb.addActionListener(new ActionListener() { 13 public void actionPerformed(ActionEvent ae) { 14 Object [] options = {"Yes please", "No thanks", "Maybe"}; 15 int value = JOptionPane.showOptionDialog(Test.this, 16 "Which option would you like?", 17 "Option Dialog", 18 JOptionPane.YES_NO_CANCEL_OPTION, 19 JOptionPane.QUESTION_MESSAGE, 20 null, // icon to display 21 options, 22 options[1]); 23 System.out.println("value = " + value); 24 if (value == 0) { 25 System.out.println("Yes please clicked"); 26 } 27 else if (value == 1) { 28 System.out.println("No thanks clicked"); 29 } 30 else if (value == 2) { 31 System.out.println("Maybe clicked"); 32 } 33 else { // value == System.out.println("X clicked"); 35 } 36 } 37 }); add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42 setSize(250, 225); 43 setLocationRelativeTo(null); 44 setVisible(true); 45 } public static void main(String args[]) { 48 Test t = new Test(); 49 } 50 } USC CSCI 201L11/30 Option Panes

Custom Option Panes ▪In most cases, the standard option panes will suffice for an application ▪If the standard option panes are too restrictive, you can always create your own using JDialog USC CSCI 201L12/30 Option Panes Image from

Outline USC CSCI 201L13/30 ▪O▪Option Panes ▪P▪Program

Program ▪Create a dialog box that pops up over an application that looks like the following. Retrieve the values of the radio buttons and the button when it is clicked. USC CSCI 201L14/30 Program Image from