Sunteți pe pagina 1din 34

Java Swing

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

2009 Syntel, Inc.

Contents
Introduction Class Hierarchy Container Controls Pluggable Look and Feel

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

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

2009 Syntel, Inc.

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

2009 Syntel, Inc.

Swing Hierarchy
Java.lang.object

JComponent (abstract)

GUI control components are concrete subclasses of this class.

JContainer (abstract)

JPanel

JWindow

JApplet

JDialog

JFrame

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

JComponent API
Customizing Component Appearance: Border, Foreground, Background, Font etc. Setting and Getting Component State

Handling Events: add Listener methods


Painting Components: repaint, revalidate Dealing with the Containment Hierarchy: add, remove Laying Out Components: getPreferedSize
9
Confidential 2009 Syntel, Inc.

Top Level Container


Swing provides three generally useful top-level container classes: JFrame, JDialog, and JApplet Points to remember
every GUI component must be part of a containment hierarchy Each GUI component can be contained only once Each top-level container has a content pane

10

Confidential

2009 Syntel, Inc.

Demo : First Swing Program

11

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

Swing Features and Concepts

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

2009 Syntel, Inc.

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

2009 Syntel, Inc.

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

2009 Syntel, Inc.

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

2009 Syntel, Inc.

What MVC facilitates?


Separating the model definition from a component facilitates model-driven programming in Swing The ability to delegate some of a component's view/controller responsibilities to separate look-and-feel objects provides the basis for Swing's pluggable look-and-feel architecture

18

Confidential

2009 Syntel, Inc.

Separable model architecture


Model Interface
ButtonModel

Component
Jbutton, JCheckBox, JRadioButton, JMenuItem

BoundedRangeModel
Document

JProgressBar, JScrollBar, JSlider


JTextField, JTextArea, JTextPane etc.

19

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

The separable model API


Implemented as a JavaBeans bound property for the component If you don't set your own model, a default is created and installed internally in the component For more complex models like Jlist or Jtable abstract model implementations are provided

21

Confidential

2009 Syntel, Inc.

Model change notification


Models must be able to notify any interested parties (such as views) when their data or value changes Swing models use the JavaBeans Event model Two Approaches for notification
Lightweight Notification a single event instance can be used for all notifications from a
particular model

Stateful Notification
describes more precisely how the model has changed

22

Confidential

2009 Syntel, Inc.

Advanced Swing Components

23
Confidential 2009 Syntel, Inc.

Split and Scroll Pane Pane


Split
Places 2 or more components side by side in a single frame Pane can horizontal or vertical

Scroll
Provide automatic scrolling facility Horizontal and vertical headers are possible

24

Confidential

2009 Syntel, Inc.

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

List model is a simple interface used to access data in the list


Demo : ListModelExample.java

Cell Rendering ListExample.java. BookCellRenderer.java


25

Confidential

2009 Syntel, Inc.

Table Component
JTable class : demo SimpleTable.java The classes and interfaces involved are
TableColumn, TableColumnModel TableModel TableCellEditor TableCellRenderer

26

Confidential

2009 Syntel, Inc.

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)

SubClasses : AbstractTableModel, DefaultTableModel

28

Confidential

2009 Syntel, Inc.

Editing and Rendering


Possible to build customized editors and renderers for the cell Default are:
Boolean - JCheckBox Number - right aligned JTextField Object - JTextField ImageIcon

29

Confidential

2009 Syntel, Inc.

Editing and Rendering (contd..)


TableCellRenderer
Componet getTableCellRendererComponent(..) Demo : FileTable2.java, FileModel.java, BigRenderer.java

TableCellEditor
Componet getTableCellEditorComponent(..)

Selecting Table Entries


Uses ListSelectionModel Row and Column selection models are different objects

30

Confidential

2009 Syntel, Inc.

Tree Terminology
Path Collapsed Expanded : A list of nodes leading from one node to another : Invisible children : Visible children

33

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

Tree Models (contd.)


MutableTreeNode defines the requirements for a tree node object that can change -- by adding or removing child nodes, or by changing the contents of a user object stored in the node

DefaultMutableTreeNode
Mutation Methods: insert, remove etc Structure Methods : provides methods to modify and querying the structure Enumeration Methods

35

Confidential

2009 Syntel, Inc.

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

2009 Syntel, Inc.

S-ar putea să vă placă și