Advanced Swing. Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.
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.
LayoutManager, J 1 Layout Manager. LayoutManager, J 2 Layout Manager To each UI container there is a layout manager (an object). When you add a component.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
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.
1 L48 Advanced GUI Component (3). 2 OBJECTIVES  To use additional layout managers.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
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.
Notes Ch. 12—Creating Tables Web Page Design. Why Use Tables? Tables are used to create a variety of items such as calendars, charts, and spreadsheets.
Using HTML to Create Tables in Web pages Connie Lindsey November 2005.
CHAPTER 11 Tables. How Are Tables Used Data Display  Very tidy and very useful Better Text Alignment  Putting text in tables allows you to format indents.
1 Chapter 22 Containers, Layout Managers, and Borders.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 5 Creating Page Templates
Actual Building the Pages Tables. Using Table Elements  To build effective page templates, you must be familiar with the HTML table elements and attributes.
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 7 Interfacing with Users.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
Domain 3 Understanding the Adobe Dreamweaver CS5 Interface.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
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.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
Lecture 10: Toolkits: Intrinsics, Callbacks, Resources, Widget hierarchies, Geometry management Brad Myers Advanced User Interface Software 1© 2013.
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
© Marty Hall, Larry Brown, Web core programming 1 Layout Managers Arranging Elements in Windows.
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.
3461 Laying Out Components Interior Design for GUIs.
Copyright Curt Hill GridBag Layout Manager A flexible but complicated layout.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 5 Creating Page Templates. Principles of Web Design 2nd Ed. Chapter 5 2 Principles of Web Design Chapter 5 Objectives Understand table basics.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Grid Bag Layout Most Complex and Powerful Components can vary in size and can be added in any order Draw out on piece of paper first Columns and Rows:
CPSC 233 Tutorial Xin Apr 6, Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
Positioning Objects with CSS and Tables
Java Swing - Lecture 3 Layout Management
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
Laying out Elements with CSS
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Positioning Objects with CSS and Tables
Working with Tabs and Tables
University of Central Florida COP 3330 Object Oriented Programming
Chap 7. Building Java Graphical User Interfaces
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Graphical User Interfaces -- Introduction
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Panels & Layout Managers
Chapter 33 Containers, Layout Managers, and Borders
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
Positioning Objects with CSS and Tables
Layout Organization and Management
Presentation transcript:

Advanced Swing

Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web Browser (Box Layout)Web Browser (Box Layout) Several other layout managers (less commonly used) – CardLayout CardLayout – SpringLayout SpringLayout – GroupLayout GroupLayout

Grid Bag Layout Rows and columns can have variable sizes Components can span multiple cells (both horizontally and vertically) Fine-grained control of how space is allocated to each component on the grid

Grid Bag Layout 1. Create an object of type GridBagLayout. You don’t need to tell it how many rows and columns the grid has (the layout manager will figure that out) 2. Set this GridBagLayout object to be the layout manager for the component 3. For each component, create an object of type GridBagConstraints. Set field values of the GridBagConstraints object to specify how the components are laid out within the grid bag 4. Finally, add each component with its constraints by using the call add(component, constraints);

Grid Bag Constraints gridx, gridy – Position of component on the grid (top-left corner) gridwidth, gridheight – Size of component (in grid cells) weightx, weighty – Controls what portion of available space the component should receive – Space is allocated to components according to their weights – A weight of zero means the component never grows or shrinks

Grid Bag Constraints fill – How should the component fill up its cell? – None, Vertical, Horizontal, Both anchor – If the component does not fill up its entire cell, where should it be placed in its cell? – Center, North, North East, West, South West, etc.

Grid Bag Constraints insets (external padding) – Surrounds the component by blank space (left, top, right, bottom) ipadx, ipady (internal padding) – Extra space added to the minimum width and height of the component – Prevents the component from ever reaching its minimum size

Grid Bag Recipe 1. Sketch out the component layout on a piece of paper. 2. Find a grid such that the small components are each contained in a single cell and the larger components span multiple cells. 3. Label the rows and columns of your grid with 0, 1, 2, 3,.... You can now read off the gridx, gridy, gridwidth, and gridheight values. 4. For each component, ask yourself whether it needs to fill its cell horizontally or vertically. If not, how do you want it aligned? This tells you the fill and anchor parameters. 5. Set all weights to 100. However, if you want a particular row or column to always stay at its default size, set the weightx or weighty to 0 in all components that belong to that row or column. 6. Write the code. Carefully double-check your settings for the GridBagConstraints. One wrong constraint can ruin your whole layout. 7. Compile and run.

Grid Bag Example Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout)

Box Layout Similar to Flow Layout, but more powerful Lay out components horizontally (in a row) or vertically (in a column) Component alignment – Column: Left, Right, Center – Row: Top, Bottom, Center Invisible components to control spacing – Border (put empty space around the entire box) – Rigid Area (fixed space between components) – Glue (“stretchy” empty space between components) – Customer Filler (empty space with min, preferred, and max sizes)

Box Layout Example: Web Browser (Box Layout)Web Browser (Box Layout) Box Layout Tutorial

Dialog Boxes JOptionPane – Built-in support for simple dialog boxes JDialog – Base class for custom dialog box windows (use instead of JFrame) Example: Web Browser (Dialog Boxes)Web Browser (Dialog Boxes) Dialog Box Tutorial

Container Components Components that contain children JPanel JSplitPane JTabbedPane – Example: WebBrowser (Panels, Splitters, & Tabs)WebBrowser (Panels, Splitters, & Tabs)

Complex Components Some components display/edit simple data values such as a string, number, or boolean – JTextField, JSlider, JCheckBox, etc. Other components display/edit entire data structures – JList, JTable, Jtree Where does such a component get the data it should display on the screen? How does such a component modify the data when it is edited by a user? Customization of how the control displays the data (customer renderers) Customization of how the control lets users edit the data (custom editing)

View Models Complex components get their data from a “view model” object The component queries the view model for the data it should display The component applies data changes to the view model The component “listens” to the view model to find out when the data has changed, and thus should be re-queried and re-displayed The “view model” gets the data from the “application model”. These two models are different, but related.

View Model Example - JTable Example: JTableJTable Initialize a JTable with the “table model” it should use The TableModel interfaceTableModel The AbstractTableModel classAbstractTableModel Example: Color Table (Basic)Color Table (Basic)

Custom Rendering Example - JTable You can tell each column in the table what “table cell renderer” to use The renderer controls how values in the column are drawn (or “rendered”) The TableCellRenderer interfaceTableCellRenderer The default table cell rendering is implemented by the DefaultTableCellRenderer classDefaultTableCellRenderer To create your own customer rendering, you can subclass DefaultTableCellRenderer and tweak its behavior Or, you can create your own class that implements the TableCellRenderer interface, and circumvent the default rendering entirely Example: Color Table (Renderer)Color Table (Renderer)

Custom Editing Example - JTable You can tell each column in the table what “table cell editor” to use The editor controls how values in the column are modified (or “edited”) The TableCellEditor interfaceTableCellEditor The default table cell editing is implemented by the DefaultCellEditor classDefaultCellEditor To create your own customer editor, you can subclass DefaultCellEditor and tweak its behavior Or, you can create your own class that implements the TableCellEditor interface, and circumvent the default editing entirely – The AbstractCellEditor base class is useful for doing thisAbstractCellEditor Example: Color Table (Editor)Color Table (Editor)

Notification of changes to the “view model” In the Color Table (Color Chooser) example, if the user right-clicks on a table cell, a color chooser is displayed, which allows the user to select a new colorColor Table (Color Chooser) – This circumvents the normal table editing process When the user selects a new color with the color chooser, the underlying data in the view model has changed, but the JTable doesn’t know that because the change didn’t happen through the table Therefore, whenever the data in the view model changes, you must call one of the “fire” methods on the view model so it will notify its listeners (i.e., the JTable) that the data has changed and needs to be re-drawn

Other complex components: JList JList displays a list of items JList Like JTable, – You feed data to a JList by implementing a “view model” class (ListModel, AbstractListModel)ListModelAbstractListModel – You can make a JList look the way you want by creating custom cell renderers (ListCellRenderer, DefaultListCellRenderer)ListCellRenderer DefaultListCellRenderer Example: Web Browser (List Favs)Web Browser (List Favs)

Other complex components: JTree JTree displays a hierarchy of folders and items JTree Like JTable, – You feed data to a JTree by implementing a “view model” class (TreeModel, DefaultTreeModel)TreeModelDefaultTreeModel – You can make a JTree look the way you want by creating custom cell (i.e., node) renderers (TreeCellRenderer, DefaultTreeCellRenderer)TreeCellRendererDefaultTreeCellRenderer – You can customize tree node editing by creating custom cell (i.e., node) editors (TreeCellEditor, DefaultTreeCellEditor)TreeCellEditor DefaultTreeCellEditor Example: Web Browser (Tree Favs)Web Browser (Tree Favs)