Java Software Solutions Foundations of Program Design Sixth Edition. by Lewis & Loftus. Coming up: GUI Components

Similar documents
Tutorial: Time Of Day Part 2 GUI Design in NetBeans

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Programming with Java GUI components

CS 335 Lecture 06 Java Programming GUI and Swing

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung


The Basic Java Applet and JApplet

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Swing. A Quick Tutorial on Programming Swing Applications

Extending Desktop Applications to the Web

GUI Event-Driven Programming

Using A Frame for Output

JiST Graphical User Interface Event Viewer. Mark Fong

SEMTech Solutions. Leaders in Refurbished SEMs. SEMTech Solutions Windows 7 SOFTWARE CONTROL SYSTEM

PostalMate Sidebar and Twinview

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Developing GUI Applications: Architectural Patterns Revisited

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

SnagIt Add-Ins User Guide

Essentials of the Java(TM) Programming Language, Part 1

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Assignment # 2: Design Patterns and GUIs

Jumble for Microsoft Outlook

National RTAP Marketing Transit Toolkit Customizing Templates in Microsoft Publisher

What is Microsoft PowerPoint?

Computing Concepts with Java Essentials

Syllabus for CS 134 Java Programming

Editors Comparison (NetBeans IDE, Eclipse, IntelliJ IDEA)

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Introduction to LogixPro - Lab

Tutorial 13: Object Animation

Everyday Excel Stuff Excel Day Planner Organizer Reference Guide

Module One: Getting Started Opening Outlook Setting Up Outlook for the First Time Understanding the Interface...

MICROSOFT OUTLOOK 2010 WORK WITH CONTACTS

Tutorial Reference Manual. Java WireFusion 4.1

Creating a Poster in PowerPoint A. Set Up Your Poster

How Scala Improved Our Java

Introduction to Visio 2003 By Kristin Davis Information Technology Lab School of Information The University of Texas at Austin Summer 2005

Windows 8.1 Tips and Tricks

Click on various options: Publications by Wizard Publications by Design Blank Publication

Mesa DMS. Once you access the Mesa Document Management link, you will see the following Mesa DMS - Microsoft Internet Explorer" window:

Choosing your Preferred Colours in Windows

Graphical User Interfaces

Microsoft Excel 2007 Level 2

How to Convert an Application into an Applet.

LogMeIn Network Console Version 8 Getting Started Guide

Getting Started on the Computer With Mouseaerobics! Windows XP

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

Creating forms in Microsoft Access 2007

Introduction To Microsoft Office PowerPoint Bob Booth July 2008 AP-PPT5

Using Flow Control with the HEAD Recorder

STEP 7 MICRO/WIN TUTORIAL. Step-1: How to open Step 7 Micro/WIN

Introduction to scripting with Unity

Windows Basics. Developed by: D. Cook

Introduction to Windows

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Essentials of the Java Programming Language

INTRODUCTION TO DESKTOP PUBLISHING

PowerPoint 2013: Basic Skills

Acrobat X Pro Accessible Forms and Interactive Documents

3D-GIS in the Cloud USER MANUAL. August, 2014

Epson Brightlink Interactive Board and Pen Training. Step One: Install the Brightlink Easy Interactive Driver

TabletWorks Help Index 1

Module 9. User Interface Design. Version 2 CSE IIT, Kharagpur

Creating Fill-able Forms using Acrobat 8.0: Part 1

How to create labels using a Microsoft Access data file?

Fireworks for Graphics and Images

Introduction to Simulink

UF Health SharePoint 2010 Introduction to Content Administration

Module 1. 4 Login-Send Message to Teacher

Handout: Word 2010 Tips and Shortcuts

Version 4.1 USER S MANUAL Technical Support (800)

Access Tutorial 6: Form Fundamentals

Web Ambassador Training on the CMS

Reassigning Key Functions on Elf or Falcon X3 Windows Mobile

HOW TO ORGANIZE PICTURES

AODA Mouse Pointer Visibility

How to make a line graph using Excel 2007

Using the Cute Rich-Text Editor

Homework/Program #5 Solutions

How To Write A Program For The Web In Java (Java)

Flash MX Image Animation

Visual Basic 2010 Essentials

Sharing Presentations, Documents, and Whiteboards

Raffle tickets How to set up the ticket body

POINT OF SALES SYSTEM (POSS) USER MANUAL

Using Microsoft Access

Cerner Update October, 2010

Task Card #2 SMART Board: Notebook

Introduction to Computers: Session 3 Files, Folders and Windows

Windows XP Pro: Basics 1

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

This Skill Builder demonstrates how to define and place sketched symbols in drawings.

Basic Formulas in Excel. Why use cell names in formulas instead of actual numbers?

Transcription:

Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components

A GUI component is an object that represents a screen element such as a button or a text field GUI-related classes are defined primarily in the java.awt and the javax.swing packages The Abstract Windowing Toolkit (AWT) was the original Java GUI package The Swing package provides additional and more versatile components 3-2

A GUI container is a component that is used to hold and organize other components A frame is a container that is used to display a GUI-based Java application A frame is displayed as a separate window with a title bar it can be repositioned and resized on the screen as needed A panel is a container that cannot be displayed on its own but is used to organize other components A panel must be added to another container to be displayed 3-3

A GUI container can be classified as either heavyweight or lightweight A heavyweight container is one that is managed by the underlying operating system A lightweight container is managed by the Java program itself Occasionally this distinction is important A frame is a heavyweight container and a panel is a lightweight container 3-4

A label is a GUI component that displays a line of text Labels are usually used to display information or identify other components in the interface Let's look at a program that organizes two labels in a panel and displays that panel in a frame This program is not interactive, but the frame can be repositioned and resized See Authority.java 3-5

Containers that contain other components make up the containment hierarchy of an interface This hierarchy can be as intricate as needed to create the visual effect desired The following example nests two panels inside a third panel note the effect this has as the frame is resized See NestedPanels.java 3-6

Images are often used in a programs with a graphical interface Java can manage images in both JPEG and GIF formats As we've seen, a JLabel object can be used to display a line of text It can also be used to display an image using the ImageIcon class ImageIcon icon = new ImageIcon ("devil.gif"); label1 = new JLabel ("Devil Left", icon, SwingConstants.CENTER); That is, a label can be composed of text, and image, or both at the same time And we can set the position of the image relative to the text 3-7

Some objects contain information that determines how the object should be represented visually Most GUI components are graphical objects We can have some effect on how components get drawn We did this in Chapter 2 when we defined the paint method of an applet Let's look at some other examples of graphical objects Coming up: Smiling Face Example

A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: components events listeners We've previously discussed components, which are objects that represent screen elements labels, buttons, text fields, menus, etc. Some components are containers that hold and organize other components frames, panels, applets, dialog boxes Coming up: Events

An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs: the mouse is moved the mouse is dragged a mouse button is clicked a graphical button is clicked a keyboard key is pressed a timer expires Events often correspond to user actions, but not always Coming up: Events and Listeners

The Java standard class library contains several classes that represent typical events Components, such as a graphical button, generate (or fire) an event when it occurs A listener object "waits" for an event to occur and responds accordingly We can design listener objects to take whatever actions are appropriate when an event occurs Coming up: Events and Listeners

Event Component Listener A component object may generate an event A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event Coming up: GUI Development

Generally we use components and events that are predefined by classes in the Java class library Therefore, to create a Java program that uses a GUI we must: 1. instantiate and set up the necessary components 2. implement listener classes for any events we care about 3. establish the relationship between listeners and components that generate the corresponding events Let's now explore some new components and see how this all comes together Coming up: Outline

A push button is a component that allows the user to initiate an action by pressing a graphical button using the mouse A push button is defined by the JButton class It generates an action event The PushCounter example displays a push button that increments a counter each time it is pushed See PushCounter PushCounterPanel.java Coming up: Push Counter Example

The components of the GUI are the button, a label to display the counter, a panel to organize the components, and the main frame The PushCounterPanel class represents the panel used to display the button and label The PushCounterPanel class is derived from JPanel using inheritance The constructor of PushCounterPanel sets up the elements of the GUI and initializes the counter to zero Coming up: Push Counter Example

The PushCounterPanel also serves as the listener for the button events. This is done by implementing the ActionListener interface: public class PushCounterPanel extends JPanel implements ActionListener Implementing the interface ActionListener says This class will define all methods defined in the ActionListener interface Anywhere you need an ActionListener, you can use an instance of this class. It is an ActionListener as well as a JPanel from Java s point of view Coming up: Push Counter Example

Listener classes are written by implementing a listener interface An interface is a list of methods that the implementing class must define The only method in the ActionListener interface is the actionperformed method The Java class library contains interfaces for many types of events We discuss interfaces in more detail in Chapter 6 Coming up: Push Counter Example

The PushCounterPanel constructor: establishes the relationship between the button and the listener by the call to addactionlistener When the user presses the button, the button component creates an ActionEvent object and calls the actionperformed method of the listener The actionperformed method increments the counter and resets the text of the label Coming up: Text Fields

Let's look at another GUI example that uses another type of component A text field allows the user to enter one line of input If the cursor is in the text field, the text field component generates an action event when the enter key is pressed See textfieldexample/fahrenheit.java textfieldexample/fahrenheitpanel.java Coming up: Fahrenheit Example

Like the PushCounter example, the GUI is set up in a separate panel class The FahrenheitPanel constructor instantiates the listener and adds it to the text field When the user types a temperature and presses enter, the text field generates the action event and calls the actionperformed method of the listener The actionperformed method computes the conversion and updates the result label End of presentation Challenge: Can you make it dynamically update the temp as you type (maybe a KeyListener?)