Documente Academic
Documente Profesional
Documente Cultură
1
Confidential 2009 Syntel, Inc.
Objective
To know Rich User Interface enhancements over AWT To understand Swing MVC To understand classes and controls available in Swing To get familiar with new Container and Controls To learn and implement pluggable look and feel
Confidential
Contents
Introduction Class Hierarchy Container Controls Pluggable Look and Feel
Confidential
What is Swing?
Swing is advertised as a set of customizable graphical components whose look-and-feel can be dictated at runtime Swing is built on top of the core 1.1 and 1.2 AWT libraries In JDK 1.1, the Swing classes must be down-loaded separately and included as an archive file on the classpath (swingall.jar). JDK 1.2 comes with a Swing distribution
Confidential
Swing features
Pluggable Look & Feel
Swing is capable of emulating several look-and-feels, and currently includes support for Windows 98 and Unix Motif
Lightweight Components
Components are not dependent on native peers to render themselves. Instead, they use simplified graphics primitives to paint themselves on the screen and can even allow portions to be transparent
Confidential
Swing Components
There are various swing components available Some of them are mentioned below:
JButton, JLabel, JTextfield, JComboBoxes etc. JTable, JList, JTree, JSliderPane, JOptionPane etc. Containers JFrame, JApplet, JPanel etc.
Confidential
Swing Hierarchy
Java.lang.object
JComponent (abstract)
JContainer (abstract)
JPanel
JWindow
JApplet
JDialog
JFrame
Confidential
JComponent Class
Tool tips: setToolTipText method Painting and borders: The setBorder method allows you to specify the border that a component displays around its edges Application-wide pluggable look and feel Double buffering: Double buffering smooths on-screen painting Key bindings
Confidential
JComponent API
Customizing Component Appearance: Border, Foreground, Background, Font etc. Setting and Getting Component State
10
Confidential
11
Confidential
Painting
JFrame- Content Pane JPanel JButton, JLabel
The top-level container, JFrame, paints itself The content pane first paints its background and it paints its border, it then tells the JPanel to paint itself, finally the panel asks its children to paint themselves
12
Confidential
13
Confidential 2009 Syntel, Inc.
Design Goals
To build a set of extensible GUI components to enable developers to more rapidly develop powerful Java front ends for commercial applications Be implemented entirely in Java to promote cross-platform consistency and easier maintenance
14
Confidential
Design Goals
Provide a single API capable of supporting multiple lookand-feels so that developers and end-users would not be locked into a single look-and-feel Enable the power of model-driven programming without requiring it in the highest-level API Adhere to JavaBeans design principles to ensure that components behave well in IDEs and builder tools
15
Confidential
Roots in MVC
MVC architecture calls for a visual application to be broken up into three separate parts:
A model that represents the data for the application The view that is the visual representation of that data A controller that takes user input on the view and translates that to changes in the model
16
Confidential
The delegate
Different view and controller became a difficult proposition So the three entities collapsed into two with a single UI (user-interface) object Component and UI delegate object
17
Confidential
18
Confidential
Component
Jbutton, JCheckBox, JRadioButton, JMenuItem
BoundedRangeModel
Document
19
Confidential
Types of Models
GUI-state models define the visual status of a GUI control
relevant only in the context of a graphical user interface (GUI) Useful if multiple GUI controls are linked to a common state e.g. shared white board programs E.g ButtonModel, BoundedRangeModel
Application-data models
represents some quantifiable data that has meaning primarily in the context of the application Like the value of a cell in a table E.g. TableModel, Document, ListModel, ComboBoxMdel
20
Confidential
21
Confidential
Stateful Notification
describes more precisely how the model has changed
22
Confidential
23
Confidential 2009 Syntel, Inc.
Scroll
Provide automatic scrolling facility Horizontal and vertical headers are possible
24
Confidential
List Component
List component consists of three parts
List data is assigned to a model i.e. ListModel User Selection : User Selection Model Visual Appearance : Cell Renderer Demo : SimpleList.java
Confidential
Table Component
JTable class : demo SimpleTable.java The classes and interfaces involved are
TableColumn, TableColumnModel TableModel TableCellEditor TableCellRenderer
26
Confidential
Table Columns
The basic unit in swing table is a column and not a cell Classes involved : TableColumn , TableColumnModel TableColumn : Its a starting point for building columns in the table Properties : cellEditor, cellRenderer,HeaderRenderer TableColumnModel manages the column selections and column spacing Demo of TableColumnModel : ColumnExample.java,SortinColumnModel.java
27
Confidential 2009 Syntel, Inc.
Table Data
The actual data thats displayed in JTable is stored in a TableModel TableModel interface : has access to all cell values in a table
columnCount, rowCount Cell methods
Object getValueAt(int, int) boolean isCellEditable(int,int) void setValueAt(Object,int,int)
28
Confidential
29
Confidential
TableCellEditor
Componet getTableCellEditorComponent(..)
30
Confidential
Tree Terminology
Path Collapsed Expanded : A list of nodes leading from one node to another : Invisible children : Visible children
33
Confidential
Tree Models
Two interfaces are particularly important
TreeModel : describes how to work with tree data TreeSelectionModel : describes how to select the modes
DefaultTreeModel class puts together a basic tree model using TreeNode objects Each nodes data is really just an Object reference, pointing to just about any object
34
Confidential
DefaultMutableTreeNode
Mutation Methods: insert, remove etc Structure Methods : provides methods to modify and querying the structure Enumeration Methods
35
Confidential
Tree Selections
Selections are based on rows and paths Path contains the list of nodes from the root of the tree to another node Rows are completely dependent on the visual display of the tree Depending on the application the row or path selection can be used
36
Confidential