Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Similar documents
DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

MIT AITI Swing Event Model Lecture 17

Swing - JButton. Adding buttons to the main window

Solution register itself

Part I: Learn Common Graphics Components

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

CS180 Recitation. More about Objects and Methods

MVC: Model View Controller

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Design Patterns and Frameworks Command

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

PIC 20A GUI with swing

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Introduction. Introduction

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Window Interfaces Using Swing. Chapter 12

CS 251 Intermediate Programming GUIs: Event Listeners

Anonymous Classes. A short-cut for defining classes when you want to create only one object of the class.

AP CS Unit 11: Graphics and Events

Graphical User Interfaces. Comp 152

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Class 16: The Swing Event Model

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Agenda. Container and Component

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Lecture (06) Java Forms

EVENTS, EVENT SOURCES AND LISTENERS

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

Systems Programming Graphical User Interfaces

CSC207 Week 4. Larry Zhang

Outline. Anonymous Classes. Annoucements. CS1007: Object Oriented Design and Programming in Java

Programming Language Concepts: Lecture 8

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphical User Interfaces in Java

6 The MVC model. Main concepts to be covered. Pattern structure. Using design patterns. Design pattern: Observer. Observers

Containers and Components

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

CMP 326 Midterm Fall 2015

Graphical User Interfaces (GUIs)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

UI Software Organization

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

The Observer Pattern

Swing from A to Z Some Simple Components. Preface

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

To gain experience using GUI components and listeners.

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Object Oriented Design & Patterns. Part 1

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Window Interfaces Using Swing Objects

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

GUI Forms and Events, Part II

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

CSE 70 Final Exam Fall 2009

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Chapter 14. More Swing

CSE 331 Software Design & Implementation

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Example: Building a Java GUI

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Java Interfaces Part 1 - Events Version 1.1

Java GUI Design: the Basics

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Design patterns for graphical user interface applications

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

1005ICT Object Oriented Programming Lecture Notes

Java.net - the Source for Java(tm) Technology Collaboration

JDirectoryChooser Documentation

Window Interfaces Using Swing Objects

Programming Languages and Techniques (CIS120)

User interfaces and Swing

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

Based on slides by Prof. Burton Ma

More About Objects and Methods

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

SampleApp.java. Page 1

More Swing. Chapter 14. Chapter 14 1

Object-Oriented Software Engineering. PersonGui (Mark 1) Case Study

Lecture 19 GUI Events

Graphical User Interface (GUI)

CS108, Stanford Handout #22. Thread 3 GUI

Programming Languages and Techniques (CIS120)

Transcription:

Commands A command is something you want someone else to perform. Instead of directly issuing a command, we can write it down and give it to someone to perform. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Command Objects In Java, we encapsulate a command in an object. The command contains a method we want another object (the receiver) to perform. Invoker perform Command Receiver

Swing Command Objects A class that implements ActionListener is a command. JButton button1 = new JButton( "Just Do It" ); button1.setactionlistener( new OnAction() ); // an inner class for invoking action class OnAction implements ActionListener { public void actionperformed(actionevent evt) { lightbulb.turnon( );

Programmable Invoker The invoker (JButton) does not need to know what command it is invoking, or what object will perform it. Just Do It JButton (invoker) onaction (performer) LightBulb (receiver)

Programmable Interfaces Use commands to "program" the user interface, instead of embedding logic haphazardly in components. Action1 Action2 Action7 Action8 A programmable remote control Action9

Calculator Application InvertCommand SqrtCommand PowerCommand UI (invoker) MultiplyCommand SubtractCommand EqualsCommand Calculator setoperand( num ) setoperator( op ) perform( ) getresult( ) (receiver)

Swing Action Swing Action interface... is ActionListener has name, icon, state contains a map for property values can be enabled and disabled <<interface>> ActionListener <<interface>> Action setenabled( ) putvalue(key,val) addpropertychange Listener( ) AbstractAction setenabled( ) putvalue(key,val)...

Using Swing Action Action on = new OnAction( ); JButton button1 = new JButton( on ); panel.add( button1 ); // to disable the JButton, write: on.setenabled( false ); /** Define an OnAction for lightbulb. */ class OnAction extends AbstractAction { public OnAction( ) { super("turn On"); public void actionperformed(actionevent evt) { receiver.seton(true);

Actions An Action is an object containing an ActionListener plus state information. Action contains a set of key-value properties. Action has a name. Action can be enabled and disabled. Useful for menus & buttons. Action can be assigned to a component. it can also be reused -- assign to many components Action makes it easy to control components.

Actions Encapsulate behavior in an object. Encapsulate properties. Component handle event set Enabled settext seticon Action icon: ImageIcon name: String properties:...

Action Can Control Many Component Encapsulate behavior in an object. Encapsulate properties. Component Component2 handle event set Enabled settext seticon Action icon: ImageIcon name: String properties:...

How to Use Action Your class extends AbstractAction. Provide a name for the action. implement actionperformed( ). associate Action with a component. if you disable the Action, the component is disabled. button.setaction( myaction ) // or button = new JButton( myaction );

Action Example Create an action for the "login" button. When user logs in, greet him then disable login button. class LoginAction extends AbstractAction { public LoginAction( String name ) { super(name); public void actionperformed( ActionEvent e ) { String name = inputfield.gettext().trim(); if (name.length() == 0) return; JOptionPane.showMessageDialog(frame, "Hello "+name); setenabled(false); // disable component inputfield.settext("");

Action Example (2) LoginAction must be an inner class so it can access the input field of GUI. Attach LoginAction to the JButton when button is created. Delete the "button.addactionlistenter(...)" public class SwingExample { private JFrame frame; private JButton button;... private void initcomponents { Action loginaction = new LoginAction("Login"); button = new JButton( loginaction );... You don't need "addactionlistener" now.

Putting Actions to Work Actions are useful for encapsulating logic and behavior We can share and reuse Actions. Example: define a LoginAction and LogoutAction. Login Action - disable input field and change button to "logout". Logout Action - enable input field and change button to "login".

Login Action class LoginAction extends AbstractAction { public LoginAction( String name ) { super(name); public void actionperformed( ActionEvent e ) { String name = inputfield.gettext().trim(); if (name.length() == 0) return; JOptionPane.showMessageDialog( frame, "Hello "+name); // change button to "logout" button.setaction( logoutaction ); inputfield.settext(""); // disable typing inputfield.setenabled(false);

Logout Action class LogoutAction extends AbstractAction { public LoginAction( String name ) { super(name); public void actionperformed( ActionEvent e ) { // display a message JOptionPane.showMessageDialog( frame, "You are logged out"); // change button to "login" button.setaction( loginaction ); // enable typing inputfield.setenabled(true);

Applying the Actions Create attributes for loginaction and logoutaction. Apply loginaction to JButton (initial state is to login). public class SwingExample { private JFrame frame; private JButton button; private Action loginaction = new LoginAction("Login"); private Action logoutaction = new LogoutAction("Logout");... private void initcomponents { button = new JButton( loginaction );...

Reusing an Action You can attach the same action to many components. Example: Make the login interface easier to use: Enable user to type his name and press ENTER. Doesn't need to click "Login" button. Solution: Apply loginaction to the JTextField. private void initcomponents { button = new JButton( loginaction ); input = new JTextField(12); input.setaction( loginaction );