Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute.
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.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
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 10: 1 TEST!!
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
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
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming, Second Edition Chapter Five Input and Selection.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
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.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
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.
Dialog Boxes.
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.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Computer Programming1 Computer Science 1 Computer Programming.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
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.
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.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Lecture Note Set 1 Thursday 12-May-05
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS18000: Problem Solving and Object-Oriented Programming
Chapter 2 - Introduction to Java Applications
Using the Java Library API
Chapter 2 - Introduction to Java Applications
Introduction to Java Programming
Chapter 2 - Introduction to Java Applications
Getting Started with Java
JOptionPane class.
F II 2. Simple Java Programs Objectives
Unit 1: Intro Lesson 4: Output.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139

The + String operation Using + to join Strings to other things is most often seen in printing, as in: System.out.println(“Welcome ” + myName + “ to Java”); You don’t have to join only strings to other strings: int myNumber = 1; System.out.println(“you are number ” + myNumber + “!!!”); And you can also use this for variables if you want: String myFirstName = “Fintan”; String mySecondName = “Costello”; String myFullName = myFirstName + “ ” + mySecondName;

Communicating with our programs We’ve already seen one way our programs can communicate with us: System.out.print(“…”) (or println(“…”) ) It would be good if we could communicate with our programs: if they could ask for values to put in variables, and we could answer. For example, in our “Welcome to java” program we had to type in the myName value as part of the program. It would be better if the program asked for the users name and gave a greeting using that value.

Communicating using a pop-up window (a ‘JOptionPane’) An “OptionPane” is a small window that pops up to ask us a question or give us some information Java lets us use OptionPanes in our programs: they are called ‘ JOptionPane ’ (the J at the beginning stands for Java) Drawing and running little pop-up windows is a complicated process Our program has to ‘import’ some extra help to do it

Program using JOptionPanes to say Hello to the user in a pop-up window /* this program creates a pop-up JOptionPane to ask your name and say Hello. It imports ‘javax.swing.JOptionPane’ to allow us use JOptionPane methods */ import javax.swing.JOptionPane; public class Hello { public static void main(String[] args) { String name; name = JOptionPane.showInputDialog(null, “What’s your name?"); JOptionPane.showMessageDialog(null, "Hello, " + name); System.exit(0); } comments class name main part of class declare a String var Put something in the variable name (what?)

New components of this program import javax.swing.JOptionPane; public class Hello { public static void main(String[] args) { String name; name = JOptionPane.showInputDialog( null, “What’s your name?”) ; JOptionPane.showMessageDialog(null, "Hello, " + name); System.exit(0); } Import the class that let us give instructions to JOptionPanes Create a MessageDialog window showing the message “ Hello ” followed by the users name (in the name variable) Ask the users name using an InputDialog window, and put the answer in variable name. Tell the System that this program is exit ing (finishing), so that it can shut down the Dialog windows properly.

What does import javax.swing.JOptionPane; mean? Java provides a number of extra “packages” to allow us to do complicated things (such as drawing windows on the screen). These packages are java code written by expert programmers. Some of these packages are named javax ( java e x tras). One of these packages is for doing things with all sorts of windows. This package is called swing. In the swing package there is a class (a program) for doing things with OptionPanes: the JOptionPane class. When we put the command import javax.swing.JOptionPane; at the start of our program, this tells the java compiler to include the code for the JOptionPane class in our program. This lets us “call” the JOptionPane program and tell it to do things with OptionPanes.

What does JOptionPane.showInputDialog(null,“ What’s your name ?”); mean? JOptionPane.showInputDialog(null,“What’s your name?”); This asks the JOptionPane programto call its showInputDialog method. The showInputDialog method is a series of commands which show an input dialog to the user (draw a window asking user to input something) The showInputDialog method draws this on the screen: and waits for the user to type something and click “ok” ? OKCancel input X What’s your name?

The arguments to JOptionPane.ShowInputDialog When we call a method, we give arguments. The method is going to do things (e.g. draw a window): the arguments are what we want it to do things with. JOptionPane.showInputDialog(null,“What’s your name?”); ? OKCancel input X What’s your handle, dude? In an input dialog, the most important argument is the question we want to ask the user in the dialog; the question they will give an answer to. JOptionPane.showInputDialog ( null,“What’s your handle, dude?”); If we change that argument, the dialog will ask a different question.

Other arguments to JOptionPane.ShowInputDialog The main argument for ShowInputDialog is the question we’re asking the user to respond to. JOptionPane.showInputDialog(null,“What’s your name?”); The other argument indicates where we want to put the pop-up window that ShowInputDialog will draw. Often we want these windows to pop up inside a “frame” (a larger window doing other things). In our simple example, we’re not using any larger windows, so we just say “null”: this means the window will just pop up in the middle of the screen. JOptionPane.showInputDialog(null,“What’s your name?”); classmethodframequery message

Where does the answer to JOptionPane.ShowInputDialog go? ? OKCancel input X What’s your name? When someone uses this, they answer the question (type their name) and click ok. Where does their name go?We can see where in the code: String name; name = JOptionPane.showInputDialog( null, “What’s your name?”) ; This means that the answer the user gives to the showInputDialog box is placed into the String variable name. The showInputDialog ‘returns’ or ‘evaluates to’ the answer the user types in the dialog box. This is always a String, so it must go in a String variable.

What about the MessageDialog? String name; name = JOptionPane.showInputDialog ( null,“What’s your name?”); This asks the showInputDialog method of the JOptionPane program to make an InputDialog window (not inside any other window) asking “What’s your name”, and puts the String that the user types as answer in the name variable. JOptionPane.showMessageDialog(null, "Hello, " + name); What does this line do? Asks the showMessageDialog method of JOptionPane to make a window (not inside any other window), saying “Hello, ”+name. No answer is returned, so no variable assignment is needed. i OK message X Hello, fintan

And finally: System.exit(0) System.exit(0); is the last line in our program. What does it do? This line sends a message to the computer’s operating System, telling it that this program (the Hello program) is exit ing (that is, shutting down). The exit value 0 tells the System, that the program ran without errors. When System gets this exit message, it makes sure that the JOptionPane program (which was called by our Hello program) is also shut down. The System.exit(0); line is like a politeness to the operating system program, telling it that the Hello program is finished using the computer now, thanks very much. In future, you should end all your programs with System.exit(0); This will be especially important if other programs are calling your programs: the System.exit(0); tells other programs when your program is finished.