CS18000: Problem Solving and Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);
Advertisements

Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
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.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
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-
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
 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.
Dialog Boxes.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
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.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
A Quick Java Swing Tutorial
INTERMEDIATE PROGRAMMING USING JAVA
Topics Graphical User Interfaces Using the tkinter Module
3 Introduction to Classes and Objects.
Outline Creating Objects The String Class Packages Formatting Output
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter Topics 15.1 Graphical User Interfaces
Objectives You should be able to describe: Interactive Keyboard Input
CSC305: COMPUTER PROGRAMMING II (JAVA)
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
Fundamentals of Python: From First Programs Through Data Structures
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
Introduction to Computing Using Java
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
Message, Input, and Confirm Dialogs
Introduction to Java Programming
Topics Graphical User Interfaces Using the tkinter Module
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Object Oriented Programming
CS431 ws99 Half Text Half Graphics
Chapter 2 - Introduction to Java Applications
JOptionPane class.
F II 2. Simple Java Programs Objectives
Presentation transcript:

CS18000: Problem Solving and Object-Oriented Programming Welcome slide for display pre-bell.

Simple Graphical User Interfaces Dialogs

Command Line vs. GUI Command line GUI (Graphical User Interface) Program prompts user for data User enters data via keyboard Output in a “terminal window” Old days: the terminal was the window GUI (Graphical User Interface) Window displays set of “controls” or “widgets” User interacts with controls Program responds to user “events” with actions We say that GUI programming is “event driven”. Instead of the program driving the user to do things in a specific order, the user drives the program to do things in his/her desired order. More or less.

Dialog Concepts Prerequisite: application must be running on JVM with window system configured Java dialogs are “modal” Application code (your program) “blocks” waiting for user response Similar to using Scanner to read from keyboard Java GUI components adapt to “Look and Feel” of local system

JOptionPane Class Java workhorse for modal dialogs Part of Java GUI package: “Swing” import javax.swing.JOptionPane; Several static methods for typical use cases… showMessageDialog showInputDialog showConfirmDialog showOptionDialog Show JOptionPaneOverview example…

JOptionPane Class Common arguments… Location where dialog pops up (null is center screen) Message to be included in dialog box (may be string or icon or html) Message type (used for “look and feel” and helpful icon) Option type (what buttons should be included by default) Options (e.g., array of Strings for button names) Icon to replace default icon of message type Title string to be used in window heading Initial value (a default value for certain option types) Many arguments can be omitted for default values

Message Type Parameter Message Type selects icon to display Look and Feel dependent Possible values JOptionPane.PLAIN_MESSAGE (-1) JOptionPane.ERROR_MESSAGE (0) JOptionPane.INFORMATION_MESSAGE (1) JOptionPane.WARNING_MESSAGE (2) JOptionPane.QUESTION_MESSAGE (3)

JOptionPane.PLAIN_MESSAGE

JOptionPane.ERROR_MESSAGE

JOptionPane.INFORMATION_MESSAGE

JOptionPane.WARNING_MESSAGE

JOptionPane.QUESTION_MESSAGE

showMessageDialog Simplest dialog At minimum, displays message to user Can include other parameters to affect appearance Only one of these methods with a void return value—it is a do-only method See ShowMessage class example.

showConfirmDialog Asks the user to confirm an action Default options: “Yes”, “No”, “Cancel” Returns int value indicating which button user selected Various button combinations available… “Yes” or “No” “OK” or “Cancel” Or user configurable with list of Strings See ShowConfirm class example.

Values with showConfirmDialog Parameter option types... JOptionPane.YES_NO_OPTION JOptionPane.YES_NO_CANCEL_OPTION JOptionPane.OK_CANCEL_OPTION Returns one of… JOptionPane.YES_OPTION (0) (same as OK_OPTION) JOptionPane.NO_OPTION (1) JOptionPane.CANCEL_OPTION (2) JOptionPane.CLOSED_OPTION (-1)

showInputDialog Asks the user for some input Returns String value Input may be… Freely typed text Selected from drop-down box or list Allows simplified arguments To create a drop-down box or list… Provide array of Strings and default value Must cast return value to String See ShowInput class example.

showOptionDialog Generalized version: configurable buttons Returns index of button selected Way too many parameters… Component parentComponent Object message String title int optionType int messageType Icon icon Object[] options Object initialValue See ShowOption class example.

Dialog Demo Code Available here: http://bit.ly/WeYeZC

Simple Graphical User Interfaces GUI Examples

Problem: CodonExtractor Write a program that reads a DNA sequence from the user and displays the codons in it Definitions: DNA sequence: sequence of chars in ACGT Codon: sequence of three chars in DNA sequence Algorithm: Prompt user for DNA, check for valid input Break DNA into 3-character chunks, display Repeat until user indicates done

CodonExtractor: main Method int continueProgram; do { // Read DNAsequence String input = JOptionPane.showInputDialog("Enter a DNA sequence"); input = input.toUpperCase(); // Make upper case String message = "Do you want to continue?"; if (isValid(input)) // Check for validity displayCodons(input); // Find and display codons else message = "Invalid DNA Sequence.\n" + message; continueProgram = JOptionPane.showConfirmDialog(null, message, "Alert", JOptionPane.YES_NO_OPTION); } while (continueProgram == JOptionPane.YES_OPTION); JOptionPane.showMessageDialog(null, "Thanks for using the Codon Extractor!");

CodonExtractor: isValid public static boolean isValid(String dna) { String validBases = "ACGT"; for (int i = 0; i < dna.length(); i++) { char base = dna.charAt(i); if (validBases.indexOf(base) == -1) return false; //base not in "ACGT" } return true;

CodonExtractor: displayCodons public static void displayCodons(String dna) { String message = ""; // Get as many complete codons as possible for (int i = 0; i < dna.length() - 2; i += 3) message += "\n" + dna.substring(i, i + 3); // 1-2 bases might be left over int remaining = dna.length() % 3; if (remaining == 1) message += "\n"+ dna.substring(dna.length() - 1, dna.length()) + "**"; else if (remaining == 2) message += "\n"+ dna.substring(dna.length() - 2, dna.length()) + "*"; message = "dna length: " + dna.length() + "\n\nCodons: " + message; JOptionPane.showMessageDialog(null, message, "Codons in DNA", JOptionPane.INFORMATION_MESSAGE); }

Problem: Prompting for a File Name JFileChooser (javax.swing.JFileChooser) Use new to create an object Set title bar with setDialogTitle(title) Show with showOpenDialog(null) Return value is an int: 0 open, 1 cancel Get the File selected with getSelectedFile() File object describes the name and location of (the path to) the file Used in Project 4.

Solution: Prompting for a File Name import java.io.File; import javax.swing.JFileChooser; public class FileChooser { public static void main(String[] args) { JFileChooser fc = new JFileChooser(); fc.setDialogTitle("Choose Important File"); int val = fc.showOpenDialog(null); System.out.println(val); File f = fc.getSelectedFile(); System.out.println(f); }