COMPSCI 230. Software Design and Construction. Swing

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

Event Driven Programming

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

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

The AWT Event Model 9

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Advanced Java Programming (17625) Event Handling. 20 Marks

SD Module-1 Advanced JAVA

Dr. Hikmat A. M. AbdelJaber

SD Module-1 Advanced JAVA. Assignment No. 4

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

GUI Software Architecture

Packages: Putting Classes Together

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

CS 251 Intermediate Programming GUIs: Event Listeners

Programming Mobile Devices J2SE GUI

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Systems Programming Graphical User Interfaces

Graphical User Interface

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

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

MIT AITI Swing Event Model Lecture 17

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Java: Graphical User Interfaces (GUI)

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

GUI Event Handlers (Part I)

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Introduction to the JAVA UI classes Advanced HCI IAT351

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!

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

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

Graphical interfaces & event-driven programming

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

GUI Programming: Swing and Event Handling

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

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

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

Programming Languages 2nd edition Tucker and Noonan"

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

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

PIC 20A GUI with swing

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Programming Languages and Techniques (CIS120)

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

Java. GUI building with the AWT

Basicsof. JavaGUI and SWING

Graphic User Interfaces. - GUI concepts - Swing - AWT

Swing from A to Z Some Simple Components. Preface

Unit 7: Event driven programming

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

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

Assignment No 2. Year: Dept.: ComputerTech. Sanjivani Rural Education Society s Sanjivani KBP Polytechnic, Kopargaon

Graphical User Interface (GUI)

Graphical User Interfaces 2

Class 16: The Swing Event Model

BM214E Object Oriented Programming Lecture 13

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

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.

Programming Languages and Techniques (CIS120e)

CS11 Java. Fall Lecture 3

Interacción con GUIs

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Lecture 19 GUI Events

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Graphical User Interface (GUI)

H212 Introduction to Software Systems Honors

CSE 331 Software Design & Implementation

JAVA. Object Oriented Programming with. M.T. Somashekara D.S.Guru K.S. Manjunatha

DM503 Programming B. Peter Schneider-Kamp.

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Java GUI Design: the Basics

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

3 Events and Listeners

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphical User Interfaces 2

We are on the GUI fast track path

Jonathan Aldrich Charlie Garrod

Bean Communication. COMP434B Software Design. Bean Communication. Bean Communication. Bean Communication. Bean Communication

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

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Original GUIs. IntroGUI 1

PESIT Bangalore South Campus

Graphical User Interfaces 2

Chapter 12 GUI Basics

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

CHAPTER 2. Java Overview

G51PRG: Introduction to Programming Second semester Applets and graphics

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programmierpraktikum

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Transcription:

COMPSCI 230 Software Design and Construction Swing 1 2013-04-17

Recap: SWING DESIGN PRINCIPLES 1. GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them) 2. Event objects and event listeners Event object: is created when event occurs (e.g. click), contains additional info (e.g. mouse coordinates) Event listener: object implementing an interface with an event handler method that gets an event object as argument 3. Separation of Model and View: Model: the data that is presented by a widget View: the actual presentation on the screen

Recap: Swing Hello World import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HelloWorld { public static void main(string[] args) { JFrame frame = new JFrame("Hello World!"); frame.setsize(220, 200); frame.setdefaultcloseoperation(jframe.exit_on_close); Container contentpane = frame.getcontentpane(); contentpane.setlayout(null); JButton button = new JButton("Hello World!"); button.setlocation(30, 30); button.setsize(150, 100); contentpane.add(button); frame.setvisible(true);

Swing Widgets JComponent void setenabled(boolean) void setvisible(boolean) void setx(int) void sety(int) void setwidth(int) void setheight(int) void setfont(font) void setforeground(color) void setbackground(color) void paint(graphics)... Container Component add(component) void remove(component)... All widgets inherit from JComponent Generic properties for position, size, colors,... Every widget adds specific properties (e.g. text for button etc.) All container widgets inherit from Container Generic methods for adding/removing children

Swing Events

Handling Events 1. When the user does something (click, mouse move, key press, etc) an event object is created 2. The event object is sent to the right target widget (depending on the mouse position or input focus) 3. Target widget has installed an event listener object for the event, using add...listener() method 4. The right handler method in the listener object is called, with the event object as argument class MyActionListener implements ActionListener { public void actionperformed( java.awt.event.actionevent e) { System.out.println("You clicked me!!!"); *

Swing Hello World with Events... public class HelloWorld { public static void main(string[] args) {... JButton button = new JButton("Hello World!"); button.addactionlistener(new MyActionListener());... import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyActionListener implements ActionListener { public void actionperformed(actionevent e) { System.out.println("You clicked me!!!"); Toolkit.getDefaultToolkit().beep();

Defining Event Listeners with Anonynous Classes... public class HelloWorld { public static void main(string[] args) {... final JButton button = new JButton("Hello World!"); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { button.setbackround(color.red); );... Use new Classname() { or new Interfacename(){ to create a single object of an anonymous subclass of the given class/interface Anonymous classes can access final variables of their context (i. e. final variables of the method or class they are created in)

Different Kinds of Swing Events Low-level events MouseEvent: Component got mouse-down, mouse-move, etc. KeyEvent: Component got key-press, key-release, etc. ComponentEvent: Component resized, moved, etc. ContainerEvent: Container's contents changed because a component was added or removed FocusEvent: Component got focus or lost focus WindowEvent: Window opened, closed, etc. High-level semantic/application events ActionEvent: Main action of control invoked (e.g. JButton click) AdjustmentEvent: Value was adjusted (e.g. JScrollBar moved) ItemEvent: Item was selected or deselected (e.g. in JList) TextEvent: Text in component has changed (e.g in JTextField)

Different Kinds of Swing Events EventObject MouseEvent ActionEvent

Events, Listeners, Adapters and Handler Methods Event Listener / Adapter Handler Methods ActionEvent ActionListener actionperformed AdjustmentEvent AdjustmentListener adjustmentvaluechanged MouseEvent MouseListener MouseAdapter mouseclicked mouseentered mouseexited mousepressed mousereleased KeyEvent KeyListener KeyAdapter keypressed keyreleased keytyped ComponentEvent ComponentListener ComponentAdapter componentshown componenthidden componentmoved componentresized Adapter classes with empty methods for Listener interfaces with >1 methods

More Widgets

Text Widgets Often there are several widgets for a certain purpose, to address slightly different requirements Usually implemented with inheritance: Base functionality in superclass Subclasses with more specific, extended functionality Common features of text widgets: Single line vs multi-line Formatting (e.g. for dates) Styling (e.g. different fonts) Multimedia objects (e.g. images, sounds, videos)

Chat Client Example Part 1 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChatDemo { public static void main(string[] args) { JFrame frame = new JFrame("My Chat Client"); frame.setsize(220, 220); frame.setdefaultcloseoperation(jframe.exit_on_close); Container contentpane = frame.getcontentpane(); contentpane.setlayout(null); final JTextArea textarea = new JTextArea(); textarea.setfont(new Font("Serif", Font.ITALIC, 16)); textarea.setlinewrap(true); textarea.setlocation(2, 0); textarea.setsize(200, 100); contentpane.add(textarea); // to be continued...

Chat Client Example Part 2 JLabel label = new JLabel("Enter message:"); label.setlocation(2, 110); label.setsize(100, 20); contentpane.add(label); final JTextField textfield = new JTextField(); textfield.setlocation(2, 130); textfield.setsize(200, 20); contentpane.add(textfield); JButton sendbutton = new JButton("Send"); sendbutton.setlocation(2, 155); sendbutton.setsize(90, 20); contentpane.add(sendbutton); JButton clearbutton = new JButton("Clear"); clearbutton.setlocation(110, 155); clearbutton.setsize(90, 20); contentpane.add(clearbutton); frame.setvisible(true); // to be continued...

Chat Client Example Part 3 sendbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { textarea.settext( textarea.gettext() + "You wrote: " + textfield.gettext() + "\n"); textfield.settext(""); textfield.requestfocus(); ); clearbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { textarea.settext(""); ); // end of main() // end of class

Summary Swing is a GUI toolkit for Java GUI as containment hierarchy of widgets Event objects and event listeners There are various widgets in the Swing API Text widgets can be used to display different kinds of information JScrollPane's can be used to fit a lot of content into a small area Menu bars can be used to organize application functions References: http://java.sun.com/docs/books/tutorial/uiswing/ http://www.javabeginner.com/java-swing-tutorial.htm

Quiz 1. What is the difference between low-level and high-level events? 2. What kind of information is found in an ActionEvent? 3. What is an anonymous class and why is it useful for specifying event handlers? 4. Write a user interface for the Tic Tac Toe game using Swing.