CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Basics of GUI Programming Chapter 11 and Chapter 22.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Aum Amriteshwaryai Namah
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
AWT.
GUI building with the AWT
GUI building with the AWT
Presentation transcript:

CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

Programming forms F Conventional/Procedural programming F code is always executed in same sequence. characterised by input/process/output F Event-driven programming F code is executed upon activation of events. sequence changes depending on order of events method 1 method 2 method method n method 1 method 2 method method n Do method 1 then method 3 Do method 2 then method 1 then method 3 Do method 2 every second

GUI USING AWT

GUI using AWT AWT - Abstract Window Toolkit Must base desktop programs on Frame constructor, paint, … browser programs on Applet init, start, paint, stop, destroy, … Can convert, but better to base code on Panel then it add to Frame or Applet

Implementing GUIs Two steps (1) Create the interface By add components & containers & using layout managers (2) Add interaction Create event listeners & “Wire-up” events

(1) Create the interface…

UI Component Layout… y x 0,0 OK , ,950 Moral… use automated Layout! OK 2 1 LayoutManager demo… See also LayoutManager Visual Guide LayoutManager Visual Guide

AWT Applications - Frame Frame is a container for components Frame with normal window controls Optional Menu Three containers in Frame with Border Layout UI-components inside containers each with own layout

AWTEvent Font FontMetrics Component Graphics ObjectColor Canvas Button TextComponent Label List CheckBoxGroup CheckBox Choice ContainerPanelApplet Frame DialogFileDialog Window TextField TextArea MenuComponent MenuItem MenuBar Menu Scrollbar LayoutManager AWT classes

Understanding the GUI UI-containers have list of UI-components Each UI-component is a class with paint method & lists of Event listeners {Frame} {label} {textfield} {button} components

Setting up the GUI Extend Frame class In constructor Create instances of containers & add them to Frame Create instances of components & add them to containers or Frame Possibly override paint method UI-components added to components list Painting Frame 1. paints Frame borders 2. calls Frame paint method 3. calls paint method of each object in component list Hint: Employ layout managers to arrange components in containers

(2) Add interaction…

Events & Event Handling Example… User clicks on a button Button is source of event object Event object passed to associated listener object Listener object executes associated method to perform desired task (save file, quit program, …) Event listener object (executes method) Event Object Event cause (mouse, keyboard, timer, …) Event Source object

Event handling… {Thermostat} set of AlarmListeners {AlarmListener} handleAlarm(-) +addAlarmListener(-) {AlarmInfo} source reading {AlarmListener} handleAlarm(-) {AlarmBell} {AlarmListener} handleAlarm(-) {AirConditioner} {Heater} {AlarmListener} Recall…

Event handling… {Thermostat} set of AlarmListeners {AlarmListener} handleAlarm(-) +addAlarmListener(-) {Heater} {AlarmInfo} source reading {Button} set of ActionListeners {ActionListener} actionPerformed(-) +addActionListener(-) {MyPanel} {ActionEvent} source …

Event handling… {Button} set of ActionListeners {ActionListener} actionPerformed(-) +addActionListener(-) {MyPanel} {ActionEvent} source … {ActionListener} actionPerformed(-) {ActionEvent} source … {MyActionListener}

Event handling… {Button} set of ActionListeners {ActionListener} actionPerformed(-) +addActionListener(-) {MyPanel} {ActionEvent} source … {ActionListener} actionPerformed(-) {MyActionListener} {ActionEvent} source … {TextField} set of ActionListeners +addActionListener(-) {ActionEvent} source …

Event handling… {Frame} set of WindowListeners windowClosing(-) +addWindowListener(-) {MyWindowListener} {WindowEvent} source … {WindowListener} {WindowAdapter} windowClosing windowClosed windowIconified windowDeiconified windowOpened etc.

Setting up Event Handling Create listener class Using new or existing class, simply Implement desired event listener interface Putting code for desired action in its methods In application (e.g. Frame) Create instance of listener class Add as listener of source object can have any number of listeners for each event Source & listener can be same object!

Understanding Events When button is pressed actionPerformed method of every item in button’s actionListeners list called Similarly for textfield When Frame close button is pressed windowClosing method of every item in Frame’s windowListeners list called {Frame} {label} {textfield} {button} components ActionListeners actionPerformed {MyListener} actionPerformed WindowListeners windowClosing

Event Classes AWTEvent EventObject AdjustmentEvent ComponentEvent TextEvent ItemEvent ActionEvent InputEvent WindowEvent MouseEvent KeyEvent ContainerEvent FocusEvent PaintEvent ListSelectionEvent

GUI USING SWING

GUI using Swing Advantages OS independent Prettier! More sophisticated components & options Pluggable “Look & feel” Borders, Tooltips, etc. Drag ‘n Drop File & ColorChoosers, Tables, editors, etc. Conceptually same as AWT Still uses AWT events package

GUI using Swing Few differences (from AWT) Use javax.swing package (equivalent Swing components start with “J”) Frames can close automatically (well sort of…!) Add components to JFrame’s contentPane (v1.5+ no longer explicitly needed) Override paintComponent, not paint (except for Jframe, JApplet & JDialog) (also, must call super.paintComponent)

AWT & Swing classes AWTEvent Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Classes in the java.awt package 1 LayoutManager * Lightweight Heavyweight

Swing - JComponents See this Visual Guide to Swing Components

DESIGNING GUI’S…

Design Tips GUI code can get very messy Do not put everything in one class (as many Visual IDE’s do) Quick & dirty = impossible to change! Employ design patterns, e.g. MVC Think Design first...

MVC - Design Pattern View model controller View Multiple Views 14:30 Half past two hours: 14 mins: 30 secs: 25 1 sec. timer Reset

Design Tips Think & design first Use layout managers Use OOP What do you want? What existing class is closest? Extend it! digital clock view - centered text in plain box, extend label analogue clock view - graphics in plain box, extend panel begin with Panel rather than Frame/Applet then add instance(s) to whichever you want

THE FUTURE… JAVAFX?

OK Junk… OK 2 1

The Java Frame class By default: Invisible Zero size Empty Has BorderLayout Plain background Doesn’t close! setVisible( true); setSize( width, height); or pack(); add( new Button() ); setLayout( new FlowLayout() ); setBackground( Color.GREEN); & override paint(Graphics) …oops

Frames in Java (1) import java.awt.*; public class GUIPlay { public static void main( String[] args) { Frame f; f = new Frame(); f.setLayout( new FlowLayout() ); f.setSize( 300, 250); f.add( new Button( “OK”) ); // pack(); f.setVisible( true); }

Frames in Java (2) import java.awt.*; public class GUIPlay { public static void main( String[] args) { Frame f; f = new MyFrame(); new MyFrame(); } import java.awt.*; public class MyFrame extends Frame { public MyFrame() { setLayout( new FlowLayout() ); setSize( 300, 250); add( new Button( “OK”) ); setVisible( true); }

Frames in Java (3) import java.awt.*; public class GUIPlay { public static void main( String[] args) { Frame f; f = new MyFrame(); new MyFrame(); } import java.awt.*; public class MyFrame extends Frame { public MyFrame() { setLayout( new FlowLayout() ); setSize( 300, 250); add( new MyPanel() ); setVisible( true); } import java.awt.*; public class MyPanel extends Panel { public MyPanel() { Button b; setBackground( Color.GREEN ); setPreferredSize( new Dimension( 200, 150) ); b = new Button( “OK”); add( b); add( new Label( “Welcome”) ); add( new TextField(20) ); }

Frames in Java (4) import java.awt.*; public class GUIPlay { public static void main( String[] args) { Frame f; f = new MyFrame(); new MyFrame(); } import java.awt.*; public class MyFrame { public MyFrame() { setLayout( new FlowLayout() ); setSize( 300, 250); add( new MyPanel() ); setVisible( true); } import java.awt.*; public class MyPanel extends Panel { public MyPanel() { Button b; setBackground( Color.GREEN ); setPreferredSize( new Dimension( 200, 150) ); b = new Button( “OK”); b.addActionListener( new MyActionListener() ); add( b); add( new Label( “Welcome”) ); add( new TextField(20) ); } import java.awt.event.*; public class MyActionListener implements ActionListener { public void actionPerformed( ActionEvent e) { System.out.println( “Button pressed”); }