1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 5: Patterns and GUI Programming CPSC 2100 Software Design and Development 1.
Java Foundation Classes (JFC/Swing) Carl Alphonce revised Spring 2007 (with contributions from Alan Hunt)
Corresponds with Chapter 12
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Hwajung Lee.
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
Graphics without NGP The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns that are.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
More OOP Design Patterns
ITEC324 Principle of CS III Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Modified from slides by Dr. Hwajung Lee.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
1 CSE 331 The Strategy and State Patterns slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Applets, AWTS CompSci 230 Software Construction.
Design Patterns and Graphical User Interfaces Horstmann ,
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
SWING IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
GoF: Document Editor Example Rebecca Miller-Webster.
Layout Manager Summary
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
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.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Java Swing - Lecture 3 Layout Management
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Chapter 5 Patterns and GUI Programming -Part 1-. Pattern A pattern is a description of a problem and its solution that you can apply to many programming.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Chapter 5 Patterns and GUI Programming. Pattern A pattern is a description of a problem and its solution that you can apply to many programming situation.
Chapter 5 Patterns and GUI Programming -Part 2-. STRATEGY Pattern Layout Managers What if we need to specifies pixel position of components when  User.
A Quick Java Swing Tutorial
Pattern and GUI Programming
Graphical User Interfaces
Provision for GUIs in Java
A Quick Java Swing Tutorial
Design Patterns in Swing and AWT
Design Patterns in Swing and AWT
<<interface>> Strategy
Composite Pattern Context:
08/15/09 Decorator Pattern Context: We want to enhance the behavior of a class, and there may be many (open-ended) ways of enhancing the class. The enhanced.
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Strategy Design Pattern
Activity 1 - Chapter 5 -.
Presentation transcript:

1 Patterns & GUI Programming Part 2

2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container Can determine your own layout pattern by implementing the LayoutManager interface

3 LayoutManager interface public interface LayoutManager { void layoutContainer(Container parent); Dimension minimumLayoutSize(Container parent); Dimension preferredLayoutSize(Container parent); void addLayoutComponent(String name, Component comp); void removeLayoutComponent(Component comp); }

4 LayoutManager methods minimumLayoutSize() and preferredLayoutSize() determine the minimum and default size of the container when components are laid out Start with preferredLayoutSize - determine the height and width of each component, and use the combined sizes to determine how large window must be Can implement minimumLayoutSize() by just having it call preferredLayoutSize()

5 LayoutManager methods layoutContainer() lays out components by setting position and size of each Method computes positions of each component then calls setBounds to put each component in its correct location

6 Strategy Pattern Context: –A class can benefit from different variants for an algorithm –Clients sometimes want to replace standard algorithms with custom versions

7 Strategy Pattern Solution –Define an interface type that is an abstraction for the algorithm –Actual strategy classes realize this interface type. –Clients can supply strategy objects –Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object

8 Strategy Pattern

9 Strategy Pattern - examples Pluggable strategy for layout management –Layout manager object responsible for executing concrete strategy –Generalizes to Strategy Design Pattern Other manifestation: Comparators Comparator comp = new CountryComparatorByName(); Collections.sort(countries, comp);

10 Layout Management - Strategy Name in Pattern –Context –Strategy –ConcreteStrategy –doWork() Actual name Container LayoutManager BorderLayout, FlowLayout, GridLayout, etc. layoutContainer()

11 Sorting - Strategy Design Name –Context –Strategy –ConcreteStrategy –doWork() Actual name Collections Comparator Comparator-implementing class compare()

12 Composite Pattern Context –Primitive objects can be combined to composite objects –Clients treat a composite object as a primitive object

13 Composite Pattern Solution –Define an interface type that is an abstraction for the primitive objects –Composite object collects primitive objects –Composite and primitive classes implement same interface type. –When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results

14 Composite Pattern

15 Containers and Components Containers collect GUI components –Sometimes, want to add a container to another container –Container should be a component Composite design pattern –Composite methods typically invoke component methods –E.g. Container.getPreferredSize invokes getPreferredSize of components

16 AWT components - composite Design name –Primitive –Composite –Leaf –method() Actual name Component Container JButton, JPanel, JOptionPane, or other class with no children Component method such as getPreferredSize()

17 Decorator Pattern Context –Component objects can be decorated (visually or behaviorally enhanced) –The decorated object can be used in the same way as the undecorated object –The component class does not want to take on the responsibility of the decoration –There may be an open-ended set of possible decorations

18 Decorator Pattern Solution –Define an interface type that is an abstraction for the component –Concrete component classes realize this interface type. –Decorator classes also realize this interface type.

19 Decorator Pattern Solution –A decorator object manages the component object that it decorates –When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

20 Decorator Pattern

21 Scroll Panes & Scroll Bars Scroll bars useful when a component contains more information than can be displayed in available space Add functionality to underlying component JScrollPane decorates a component and is itself a component

22 JScrollPane  Scroll bars can be attached to components  Approach #1: Component class can turn on scroll bars  Approach #2: Scroll bars can surround component JScrollPane pane = new JScrollPane(component);  Swing uses approach #2 - JScrollPane is again a component

23 Decorator Pattern: Scroll Bars Design name: –Component –ConcreteComponent –Decorator –method() Actual name: Component JTextArea JScrollPane paint() or other method of Component

24 I/O stream filters as decorators BufferedReader takes a Reader and adds buffering Result is another Reader: Decorator pattern InputStreamReader r = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(r); Many other decorators in stream library, e.g. PrintWriter

25 Decorator pattern: I/O filters Design name: –Component –ConcreteComponent –Decorator –method() Actual name: Reader InputStreamReader BufferedReader read()

26 How to Recognize Patterns Look at intent of pattern: e.g. COMPOSITE has different intent than DECORATOR Remember common uses (e.g. STRATEGY for layout managers) Use context and solution as "litmus test"

27 Recognizing Patterns Not everything that is strategic is an example of STRATEGY pattern –Context must want to use different variants of an algorithm –Must be an interface type that is an abstraction of the algorithm; concrete strategy classes must implement the interface –Client supplies object of strategy class to context class; context class uses strategy object to invoke algorithm

28 Litmus test - example  Can add border to Swing component Border b = new EtchedBorder() component.setBorder(b); Undeniably decorative Is it an example of DECORATOR?

29 Litmus test - example Component objects can be decorated (visually or behaviorally enhanced): PASS The decorated object can be used in the same way as the undecorated object: PASS The component class does not want to take on the responsibility of the decoration: FAIL--the component class has setBorder method