CS 3331 1 GUI Frameworks CS 3331 Fall 2009. 2 CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.

Slides:



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

Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
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.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Lecture 19 Graphics User Interfaces (GUIs)
Intermediate Java1 An example that uses inner classes Week Four Continued.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
Io package as Java’s basic I/O system continue’d.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Programming in Java
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components: Part 1 Chapter 11.
 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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
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.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
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,
Computer Science 209 GUIs Model/View/Controller Layouts.
Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Welcome To java
Object-Orientated Analysis, Design and Programming
CHAPTER Reacting to the user.
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Swing JComponents.
GUIs Model/View/Controller Layouts
Review: Java GUI Programming
A Quick Java Swing Tutorial
Ellen Walker Hiram College
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Programming Graphical User Interface (GUI)
GUI building with the AWT
Presentation transcript:

CS GUI Frameworks CS 3331 Fall 2009

2 CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events

3 CS 3331 MVC Model A way of cleanly breaking an application into three parts  Model for maintaining data,  View for displaying all or a portion of the data, and  Controller for handling events that affect the model or views. Model ControllerView

4 CS 3331 Separating M from VC Controller changes model or view get data from model update view when data changes ViewModel

5 CS 3331 Due to Separation … Multiple views and controllers for models gui.ppt change notification request & modification views model

6 CS 3331 Due to Separation … (Cont.) New views and controllers can be added for the model. Changes in views are localized; such changes have minimal or no impact on the model.

7 CS 3331 Outline  MVC Model GUI components (widgets) Layout managers Handling events

8 CS 3331 Sample GUI Program Counter applet Increment 10 Value:

9 CS 3331 Java GUI Frameworks GUI frameworks  Part of Java Foundation Class (JFC)  Abstract Window Toolkit (AWT) and Swing Framework classes  GUI components (or widgets)  Layout managers  Events and event listeners  Graphics and imaging classes

10 CS 3331 AWT vs. Swing AWT  Heavyweight components  Associated with native components called peers  Same behaviour, but platform-dependent look  Package java.awt Swing  Lightweight components, i.e., no peer components  Same look and feel across platforms  Support pluggable look and feel  Package javax.swing

11 CS 3331 AWT Components primitive container

12 CS 3331 Swing Components Every widget is a container!

13 CS 3331 Composite Design Pattern To allow clients to treat both single components and collections of components identically To define recursive data structures such as trees Client uses Leaf operation() Component operation() Composite operation() add(Component) remove(Component) *

14 CS 3331 Exercise Design a class hierarchy to represent parse trees for: Expression ::= Literal | Expression “+” Expression

15 CS 3331 Outline  MVC Model  GUI components (widgets) Layout managers Handling events

16 CS 3331 Q: How to Layout Widgets? Three approaches  Manually specify absolute positions  Manually specify relative positions  Automate it What’re pros and cons?

17 CS 3331 Layout Managers Associated with containers Automate the layout of elements  When elements are added to the container  When the window is resized automatically adjust the positions and sizes of the elements.

18 CS 3331 Hierarchy of Layout Managers Q: Can you identify the design pattern used here?

19 CS 3331 Using Layout Managers Method Description setLayout(lm) Set lm as the layout manager add(comp) Add a component add(comp, cst) Add a component with constraint public class CounterApplet extends Applet { public CounterApplet () { setLayout(new FlowLayout()); add(new JButton(“Increment")); add(new JButton(“Decrement”)); }

20 CS 3331 Flow Layout width=400 height=50 width=100 height=120

21 CS 3331 Flow Layout (Cont.) public class Flow extends Applet { public Flow () { setLayout(new FlowLayout()); add(new JButton("Java")); add(new JButton("C++")); add(new JButton("Perl")); add(new JButton("Ada")); add(new JButton("Smalltalk")); add(new JButton("Eiffel")); }

22 CS 3331 Grid Layout 3x2 grid 1x0 grid 0x1 grid

23 CS 3331 Grid Layout (Cont.) public class Grid extends Applet { public void init () { int row = 0; int col = 0; String att = getParameter("row"); if (att != null) { row = Integer.parseInt(att); } att = getParameter("col"); if (att != null) { col = Integer.parseInt(att); } if (row == 0 && col == 0) { row = 3; col = 2; } setLayout(new GridLayout(row, col)); add(new JButton("Java")); add(new JButton("C++")); add(new JButton("Perl")); add(new JButton("Ada")); add(new JButton("Smalltalk")); add(new JButton("Eiffel")); }

24 CS 3331 Question Why compose the GUI in the init() method not in the constructor?

25 CS 3331 Border Layout

26 CS 3331 Border Layout (Cont.) public class Border extends Applet { public Border () { setLayout(new BorderLayout()); add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("East"), BorderLayout.EAST); add(new JButton("West"), BorderLayout.WEST); add(new JButton("Center"), BorderLayout.CENTER); }

27 CS 3331 Exercise Write an applet of the following layout. public class ClassRoom extends Applet { public ClassRoom () { setLayout(new BorderLayout()); // WRITE YOUR CODE HERE… }

28 CS 3331 Outline  MVC Model  GUI components (widgets)  Layout managers Handling events

29 CS 3331 Event Handling Mechanism to write control code Composed of:  Event  Event source  Event listener (or handler)

30 CS 3331 Event Handling (Cont.) Event  A way for GUI components to communicate with the rest of application  Implemented as event classes (e.g., ActionEvent) Event source  Components generating events  Examples: buttons, check boxes, combo boxes, etc.

31 CS 3331 Event Handling (Cont.) Event listener (or handler)  Objects that receives and processes events  Must implement an appropriate listener interface  Must inform the source its interest in handling a certain type of events (by registering)  May listen to several sources and different types of events

32 CS 3331 Example // create a button JButton button = new JButton(“Increment”); // register an action listener button.addActionListener(new ButtonActionListener()); // Action listener class class ButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // handle the event e … System.out.println(“Increment button pressed!”); }

33 CS 3331 How Does It Work? > h: ActionListenerb: JButtone: ActionEvent addActionListener(h) actionPerformed(e) getSource()

34 CS 3331 Naming Convention For event XYZ …  Event class: XYZEvent  Listener interface: XYZListener  Adapter class: XYZAdapter  Registration method: addXYZListener()

35 CS 3331 Events and Listeners Event Listener Adapter ActionEvent ActionListener ComponentEvent ComponentListenerComponentAdapter FocusEvent FocusListenerFocusAdapter KeyEvent KeyListenerKeyAdapter MouseEvent MouseListenerMouseAdapter MouseMotionListener MouseMotionAdapter WindowEvent WindowListenerWindowAdapter ItemEvent ItemListener TextEvent TextListener …

36 CS 3331 Example: Resizing Component To prevent windows from being resized too small, use ComponentEvent and ComponentListener public class WinJava extends JFrame { public WinJava(String name) { super(name); // …. setResizable(true); addComponentListener(Util.createComponentListener(400, 300); } // … }

37 CS 3331 Example (Cont.) public class Util { public static ComponentListener createComponentListener(int width, int height) { return new MyComponentListener(width, height); } private static class MyComponentListener extends ComponentAdapter { private int width, height; public MyComponentListener(int w, int h) { width = w; height = h; } public void componentResized(ComponentEvent e) { Component c = e.getComponent(); if (c.getWidth() < width || c.getHeight() < height) { c.setSize(Math.max(width, c.getWidth()), Math.max(height, c.getHeight())); } } // MyComponentListener }

38 CS 3331 Using Anonymous Class // same code with anonymous class public class Util { public static ComponentListener createComponentListener( final int width, final int height) { return new ComponentAdapter() { public void componentResized(ComponentEvent e) { Component c = e.getComponent(); if (c.getWidth() < width || c.getHeight() < height) { c.setSize(Math.max(width, c.getWidth()), Math.max(height, c.getHeight())); } } // componentResized }; // ComponentAdapter } // createComponentListener }

39 CS 3331 Exercise Write handler code by using anonymous class to print a goodbye message to System.out when the main window is closed. Hint: The WindowListener interface defines, among others, the method “void windowClosing(WindowEvent)”. public class WinJava extends JFrame { public WinJava(String name) { // …. // WRITE YOUR CODE HERE } // … }

40 CS 3331 Exercise Extend the counter applet to change its button color when the mouse enters the button. Hints - The interface MouseListener declares, among others, void MouseEntered(MouseEvent) and void mouseExited(MouseEvent). - The method setBackground(Color) sets the background color of a widget. - The source (e.g., JButton) can be obtained from an event object by calling the method “Object getSource()”.

41 CS 3331 Exercise (Cont.) public CounterApplet extends Applet { public CounterApplet() { // … JButton button = new JButton("Increment"); // WRITE YOUR CODE HERE! // … } // … }