Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

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.
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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
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.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
GUI Programming in Java
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
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.
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.
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.
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.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
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.
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
GUIs Model/View/Controller Layouts
Review: Java GUI Programming
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
GUI Programming III: Events
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Programming Graphical User Interface (GUI)
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

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

3 What’s a Framework? A set of cooperating (abstract) classes and interface that:  Represent reusable designs of software systems in a particular application domain,  Provide semi-complete applications that can be specialized to produce custom applications, and  Defines conventions for extending the abstract classes, implementing the interfaces, and allowing their instances to interact with one another

4 Examples Small frameworks  Applets, AnimationApplet, DBAnimationApplet, etc. GUI frameworks  AWT and Swing  MFC Distributed programming frameworks  RMI  J2EE  CORBA

5 Why Frameworks? Main goal is:  To support reuse of design and implementations in particular application domains Benefits  Quick and easy application development  Reliable application (i.e., high confidence)  Standardized applications

6 Characteristics Extensibility Inversion of control Design patterns as building blocks

7 Questions How frameworks differ from conventional OO libraries? How frameworks differ from design patterns?

8 Frameworks To Be Discussed GUI frameworks (AWT and Swing) Collection classes Input and output classes

9 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

10 Separating M from VC Controller changes model or view get data from model update view when data changes ViewModel

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

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

13 Outline  MVC Model GUI components (widgets) Layout managers Handling events

14 Sample GUI Program Counter applet Increment 10 Value:

15 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

16 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

17 AWT Components primitive container

18 Swing Components Every widget is a container!

19 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) *

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

21 Outline  MVC Model  GUI components (widgets) Layout managers Handling events

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

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

24 Hierarchy of Layout Managers Q: Can you identify the design pattern used here?

25 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”)); }

26 Flow Layout width=400 height=50 width=100 height=120

27 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")); }

28 Grid Layout 3x2 grid 1x0 grid 0x1 grid

29 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")); }

30 Question Why compose the GUI in the init() method not in the constructor?

31 Border Layout

32 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); }

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

34 Outline  MVC Model  GUI components (widgets)  Layout managers Handling events

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

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

37 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

38 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!”); }

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

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

41 Events and Listeners Event Listener Adapter ActionEvent ActionListener ComponentEvent ComponentListenerComponentAdapter FocusEvent FocusListenerFocusAdapter KeyEvent KeyListenerKeyAdapter MouseEvent MouseListenerMouseAdapter MouseMotionListener MouseMotionAdapter WindowEvent WindowListenerWindowAdapter ItemEvent ItemListener TextEvent TextListener … ComponentEvent: component’s changes of size, location, and visibility FocusEvent: component’s gain/loss of keyboard focus KeyEvent: keystrokes MouseEvent: mouse press, release, click, enter and exit; mouse motion (e.g., dragging and moving) WindowEvent: opened, closed, activated or deactivated, iconified or deiconified. ItemEvent: item selection, etc.

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

43 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 }

44 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 }

45 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 Main extends JFrame { public Main(String name) { // …. // WRITE YOUR CODE HERE } // … }

46 Exercise: Mouse Motion Event 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()”.

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

48 Exercise: JavaChat Create UI of JavaChat dialog (sample screens below); dialog template available from course website. Upon the Send button click when not connected (left) and connected (right) UI components: JButton, JTextField, JTextArea, JScrollPane