Programming Mobile Devices J2SE GUI

Similar documents
GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

GUI in Java TalentHome Solutions

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

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

COMPSCI 230. Software Design and Construction. Swing

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

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

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

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

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

Java AWT Windows, Text, & Graphics

CS 251 Intermediate Programming GUIs: Components and Layout

Sri Vidya College of Engineering & Technology

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

The AWT Event Model 9

encompass a group of features for building Graphical User Interfaces (GUI).

Chapter 1 GUI Applications

Packages: Putting Classes Together

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...

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

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

SD Module-1 Advanced JAVA

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

SD Module-1 Advanced JAVA. Assignment No. 4

Java Applets / Flash

Introduction to the JAVA UI classes Advanced HCI IAT351

BM214E Object Oriented Programming Lecture 13

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSC207H: Software Design Lecture 11

Unit 7: Event driven programming

Advanced Java Programming (17625) Event Handling. 20 Marks

Computer Programming Java AWT Lab 18

Basicsof. JavaGUI and SWING

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

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

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

Window Interfaces Using Swing Objects

User interfaces and Swing

Advanced Java Programming

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

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

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

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

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

CompSci 230 S Programming Techniques. Basic GUI Components

The AWT Package, An Overview

GUI Programming: Swing and Event Handling

G51PRG: Introduction to Programming Second semester Applets and graphics

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

CS 2113 Software Engineering

Java. GUI building with the AWT

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!

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

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

Part 3: Graphical User Interface (GUI) & Java Applets

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

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

Graphical User Interfaces 2

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

DM503 Programming B. Peter Schneider-Kamp.

Window Interfaces Using Swing Objects

Frameworks. CS151 Chris Pollett Oct. 26, 2005.

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

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

Class 16: The Swing Event Model

PIC 20A GUI with swing

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

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

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

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

user-friendly and easy to use.

Part I: Learn Common Graphics Components

Lecture 5: Java Graphics

Laying Out Components. What is Widget Layout?

Building Graphical User Interfaces. GUI Principles

Contents Introduction 1

(Incomplete) History of GUIs

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

FirstSwingFrame.java Page 1 of 1

Graphical User Interfaces in Java - SWING

Windows and Events. created originally by Brian Bailey

GUI Event Handlers (Part I)

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

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

Building Graphical User Interfaces. Overview

CHAPTER 2. Java Overview

SWING - GROUPLAYOUT CLASS

Graphical User Interfaces. Comp 152

Handling Mouse and Keyboard Events

2110: GUIS: Graphical User Interfaces

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

Programming Language Concepts: Lecture 8

Graphical Interfaces

Transcription:

Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at

Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere (WOTE) instead of write once, run everywhere (WORE) SWING solve AWT shortcomings 2

High-Level: Abstract Windows Toolkit (AWT) The original Java GUI toolkit Comes standard with every version of Java SE/EE It is very stable everywhere were you find a Java runtime environment, your program will work (in theory) Simple: limited GUI components, layout managers, and events (e.g. not supported: Tables, Trees, Progress Bars, etc.) 3

The basic AWT Class tree All in java.awt package Object CheckboxGroup *Component Button Canvas CheckBox Choice Container Panel Applet ScrollPane Window Dialog Frame Label List TextComponent TextArea TextField MenuComponent MenuItem CheckboxMenuItem Menu PopupMenu "*" indicates abstract 4

Steps to create any GUI application or applet 1. Compose a GUI by adding components to a Container object 2. Setup event handlers to respond to user interactions with the GUI 3. Display the GUI (automatically done for applets, you must explicitly do this for applications). 5

Simple AWT Application import java.awt.*; public class AButtonPanel extends Frame { public AButtonPanel() { // STEP 1: Compose the GUI super("basicapplication Title"); Button uselessbutton = new Button("Useless Button"); add(uselessbutton); // STEP 2: Setup Event handlers addwindowlistener( new WindowAdapter() { public void windowclosing(windowevent e) { setvisible(false); dispose(); System.exit(0); ); public static void main(string[] args) { AButtonPanel panel = new AButtonPanel(); // STEP 3: Display the GUI (automatic in applets) panel.setsize(100, 100); panel.setvisible(true); 6

Selected AWT Components (1) Checkbox import java.applet.applet; import java.awt.checkbox; public class CheckboxTest extends Applet { public void init() { Checkbox checkbox = new Checkbox("Simple checkbox"); checkbox.setstate(true); add(checkbox); 7

Selected AWT Components (2) Choice import java.applet.applet; import java.awt.choice; public class ChoiceTest extends Applet { public void init() { Choice choice = new Choice(); choice.add("1975"); choice.add("1976"); choice.add("1977"); add(choice); 8

Selected AWT Components (3) TextArea import java.applet.applet; import java.awt.textarea; public class TextAreaTest extends Applet{ public void init() { TextArea area = new TextArea("Initial text goes here", 10, 30); add(area); 9

Common Component Methods getheight(); getwidth(); getx(); gety(); getlocationonscreen(); setenabled(boolean); setvisible(boolean); setbackground(color); setforeground(color); setfont(font) 10

AWT Event Handling Objects register as listeners for events All listeners are always notified How it works: Components generate subclasses of AWTEvent to notify listensers about state changes Any class can be a listener using addxxlistener() You have to implement the listener type Some listeners require to implement multiple methods. There are adapters that implement the listener interfaces and stub out all the methods 11

Low-level Events (basics) ComponentEvent Hiding, moving, resizing, showing ContainerEvent FocusEvent KeyEvent MouseEvent WindowEvent Adding/removing component Getting/losing focus Pressing, releasing, or typing (both) a key Clicking, dragging, entering, exiting, moving, pressing, or releasing Iconifying, deiconifying, opening, closing, really closed, activating, deactivating 12

AWT Event Hierarchy Most in java.awt package Object EventObject AWTEvent ActionEvent AdjustmentEvent ComponentEvent ContainerEvent FocusEvent InputEvent KeyEvent MouseEvent MouseWheelEvent PaintEvent WindowEvent HierarchyEvent InputMethodEvent InvocationEvent ItemEvent TextEvent 13

AWT Event Listeners Example: MouseListener mouseclicked(mouseevent e) mouseentered(mouseevent e) mouseexited(mouseevent e) mousepressed(mouseevent e) mousereleased(mouseevent e) import java.awt.event.mouseadapter; import java.awt.event.mouseevent; public class MyMouseAdapter extends MouseAdapter { public void mouseclicked(mouseevent e) { System.out.println("Mouse Clicked"); 14

Listeners Mouse Example import java.applet.applet; import java.awt.*; import java.awt.event.*; public class Draw extends Applet { public void init() { addmouselistener( new MouseAdapter() { // inline int savedx, savedy; public void mousepressed(mouseevent e) { savedx = e.getx(); savedy = e.gety(); public void mousereleased(mouseevent e) { Graphics g = Draw.this.getGraphics(); g.drawrect(savedx, savedy, e.getx()-savedx, e.gety()-savedy); ); 15

AWT Layout Manager A Layout Manager is an object that controls the size and position (layout) of components inside a container object All in java.awt package, * indicates interface *LayoutManager FlowLayout GridLayout *LayoutManager2 BorderLayout CardLayout GridBagLayout 16

Layouts (1) BorderLayout JButton button = new JButton("Button 1 (PAGE_START)"); pane.add(button, BorderLayout.PAGE_START); button = new JButton("Button 2 (CENTER)"); button.setpreferredsize(new Dimension(200, 100)); pane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); pane.add(button, BorderLayout.LINE_START); button = new JButton("Long-Named Button 4 (PAGE_END)"); pane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); pane.add(button, BorderLayout.LINE_END); 17

Layouts (2) GridLayout pane.setlayout(new GridLayout(0,2)); pane.add(new JButton("Button 1")); pane.add(new JButton("Button 2")); pane.add(new JButton("Button 3")); pane.add(new JButton("Long-Named Button 4")); pane.add(new JButton("5")); 18

Layouts (3) FlowLayout contentpane.setlayout(new FlowLayout()); contentpane.add(new JButton("Button 1")); contentpane.add(new JButton("Button 2")); contentpane.add(new JButton("Button 3")); contentpane.add(new JButton("Long-Named Button 4")); contentpane.add(new JButton("5")); 19

Low-Level: SWING how it differs Provide a more polished look and feel than the standard AWT component set A subset of SWING widgets is analogous to the basic AWT widgets (they are lightweight components, rather than peer-based components) - separation of model from the view and controller Additional components e.g. trees, tabbed panes Programmable look and feel Renderers and editors typically starting with "J" (e.g. JButton) 20

SWING similarities with AWT at the Component level 21

22 SWING differences to AWT

Example: do Label and JLabel really differ? Additional functionality of JLabel: Add an Icon Set the vertical and horizontal position of text relative to the Icon Set the relative position of contents within component 23

Example JLabel in Action public class LabelPanel extends JPanel { public LabelPanel() { // Create and add a JLabel JLabel plainlabel = new JLabel("Plain Small Label"); add(plainlabel); // Create a 2nd JLabel JLabel fancylabel = new JLabel("Fancy Big Label"); // Instantiate a Font object to use for the label Font fancyfont = new Font("Serif", Font.BOLD Font.ITALIC, 32); // Associate the font with the label fancylabel.setfont(fancyfont); // Create an Icon Icon tigericon = new ImageIcon("SmallTiger.gif"); // Place the Icon in the label fancylabel.seticon(tigericon); // Align the text to the right of the Icon fancylabel.sethorizontalalignment(jlabel.right); // Add to panel add(fancylabel); 24

Exercise 1 Listeners + Layouts Write an J2SE application or applet that displays the x,y location of the last mouse click and provides a button to reset the displayed x,y coordinates to 0,0 25