1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
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.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
1 CSE 403 Design Patterns and GUI Programming Reading: Object-Oriented Design and Patterns, Ch. 5 (Horstmann) These lecture slides are copyright (C) Marty.
1 TCSS 143, Autumn 2004 Lecture Notes Graphical User Interfaces Koffman/Wolfgang Appendix C, pp
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
1 CSE 403 Design Patterns and GUI Programming Reading: Object-Oriented Design and Patterns, Ch. 5 (Horstmann) These lecture slides are copyright (C) Marty.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Software Constructions LAB 09 Java Programming with SWING GUI Builder-Part III.
Computer Science 209 GUIs Model/View/Controller Layouts.
Visual Index of Swing GUI Components
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Graphical User Interface (GUI)
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.
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:
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Chapter 7 A First Look at GUI Applications Layout Managers.
A Quick Java Swing Tutorial
Graphical User Interfaces
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
A Quick Java Swing Tutorial
Timer class and inner classes
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Panels & Layout Managers
Chapter 7-3 (Book Chapter 14)
UNIT-III Structural Design Patterns
A Quick Java Swing Tutorial
CSE 403 Design Patterns and GUI Programming Reading:
Layout Organization and Management
Presentation transcript:

1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia

2 Pattern: Composite objects that can contain their own type

3 Containers and layout Place components in a container; add the container to a frame.  container: An object that stores components and governs their positions, sizes, and resizing behavior.

4 Complex layout... how? How would you create a complex layout like this, using only the layout managers shown?

5 Composite pattern composite: An object that can be either an individual item or a collection of many items.  Can be composed of individual items or other composites.  Recursive definition: Objects that can hold themselves.  Often leads to a tree structure of leaves and nodes: ::= | ::= * Examples in Java:  collections (e.g. a list of lists)  GUI layout (containers of containers of components)

6 Composite layout composite layout: One made up of containers within containers. Each container has a different layout, and by combining the layouts, more complex / powerful layout can be achieved.  Example: A flow layout in the south region of a border layout.  Example: A border layout in square (1, 2) of a grid layout. In the GUI at right:  How many containers are there?  What layout is used in each?

7 JPanel the default container class in Swing public JPanel() public JPanel(LayoutManager mgr) Constructs a panel with the given layout (default = flow layout). public void add(Component comp) public void add(Component comp, Object info) Adds a component to the container, possibly giving extra information about where to place it. public void remove(Component comp) public void setLayout(LayoutManager mgr) Uses the given layout manager to position components.

8 Flow, Border, Grid layouts Container panel1 = new JPanel(new FlowLayout()); panel1.add(new JButton("Button 1")); panel1.add(new JButton("Button 2")); Container panel2 = new JPanel(new BorderLayout()); panel2.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); Container panel3 = new JPanel(new GridLayout(3, 2)); panel3.add(new JButton("Button 1")); panel3.add(new JButton("Button 2"));

9 Box layout Container box1 = Box.createHorizontalBox(); Container box2 = Box.createVerticalBox(); aligns components in container in a single row or column components use preferred sizes and align based on their preferred alignment  vertical box is used to get a "vertical flow layout"

10 Other layouts CardLayout Layers of "cards" stacked on top of each other; one visible at a time. GridBagLayout Powerful, but very complicated; Our recommendation: never use it. null layout allows you to define absolute positions using setX/Y and setWidth/Height (not recommended; platform dependent)

11 Composite layout code Container north = new JPanel(new FlowLayout()); north.add(new JButton("Button 1")); north.add(new JButton("Button 2")); Container south = new JPanel(new BorderLayout()); south.add(new JLabel("Southwest"), BorderLayout.WEST); south.add(new JLabel("Southeast"), BorderLayout.EAST); // overall panel contains the smaller panels (composite) Container overall = new JPanel(new BorderLayout()); overall.add(north, BorderLayout.NORTH); overall.add(new JButton("Center"), BorderLayout.CENTER); overall.add(south, BorderLayout.SOUTH); frame.add(overall);

12 Pattern: Decorator objects that "wrap" other objects to add features

13 JTextField, JTextArea an input control for typing text values (field = single line; area = multi-line) public JTextField(int columns) public JTextArea(int lines, int columns) Creates a new field, the given number of letters wide. public String getText() Returns the text currently in the field. public void setText(String text) Sets field's text to be the given string.  What if the text area is too big to fit in the window?

14 JScrollPane a container that adds scrollbars around any other component public JScrollPane(Component comp) Wraps the given component with scrollbars.  After constructing the scroll pane, you must add the scroll pane, not the original component, to the onscreen container: myContainer.add(new JScrollPane(textarea), BorderLayout.CENTER);

15 Decorator pattern decorator: An object that modifies behavior of, or adds features to, another object.  Must maintain the common interface of the object it wraps up.  Used so that we can add features to an existing simple object without needing to disrupt the interface that client code expects when using the simple object.  The object being "decorated" usually does not explicitly know about the decorator. Examples in Java:  Multilayered input streams adding useful I/O methods  Adding scroll bars to GUI controls

16 Decorator example: I/O normal InputStream class has only public int read() method to read one letter at a time decorators such as BufferedReader or Scanner add additional functionality to read the stream more easily // InputStreamReader/BufferedReader decorate InputStream InputStream in = new FileInputStream("hardcode.txt"); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); // because of decorator streams, I can read an // entire line from the file in one call // (InputStream only provides public int read() ) String wholeLine = br.readLine();

17 Decorator example: GUI JScrollPane is a container with scroll bars to which you can add any component to make it scrollable // JScrollPane decorates GUI components JTextArea area = new JTextArea(20, 30); JScrollPane sp = new JScrollPane(area); contentPane.add(sp);  Components also have a setBorder method to add a "decorative" border. Is this another example of the Decorator pattern? Why or why not?

18 JOptionPane JOptionPane.showMessageDialog( parent, message ); import javax.swing.*; JOptionPane.showMessageDialog(null, "This candidate is a dog. Invalid vote."); Advantages:  Simple; looks better than console. Disadvantages:  Created with static methods; not object-oriented.  Not powerful (just simple dialog boxes).

19 More JOptionPane JOptionPane.showConfirmDialog( parent, message )  Displays a message and list of choices Yes, No, Cancel.  Returns an int such as JOptionPane.YES_OPTION or NO_OPTION to indicate what button was pressed. JOptionPane.showInputDialog( parent, message )  Displays a message and text field for input.  Returns the value typed as a String (or null if user presses Cancel).