Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...

Chapter 1. (Introduction) Swing Overview. Easy for reading, Click here!

Custom Search
Chapter 1. (Introduction) Swing Overview. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 3/3 

Previous Page Previous Page (2/3)
Subpages: 1. Chapter 1. Swing Overview. AWT, Swing 
MVC architecture: Model, View, Controller 
3.  Custom models - II, UI delegates and PLAF 

1.4    UI delegates and PLAF

Almost all modern user interface frameworks coalesce the view and controller, whether they are based on SmallTalk, C++, and now Java. Examples include MacApp, Smalltalk/V, Interviews, and the X/Motif widgets used in IBM Smalltalk.[1] JFC Swing is the newest addition to this crowd. Swing packages each component's view and controller into an object called a UI delegate. For this reason Swing's underlying architecture is more accurately referred to as model-delegate rather than model-view-controller. Ideally communication between both the model and the UI delegate is indirect, allowing more than one model to be associated with one UI delegate, and vice versa. Figure 1.6 illustrates.

Figure 1.6 Model-delegate architecture

<<file figure1-6.gif>>

1.4.1    The ComponentUI class

Each UI delegate is derived from an abstract class called ComponentUI. ComponentUI methods describe the fundamentals of how a UI delegate and a component using it will communicate. Note that each method takes a JComponent as parameter.

ComponentUI methods:

  static ComponentUI CreateUI(JComponent c)

This is normally implemented to return a shared instance of the UI delegate defined by the defining ComponentUI subclass itself. This instance is used for sharing among components of the same type (e.g. All JButtons using the Metal look-and-feel share the same static UI delegate instance defined in javax.swing.plaf.metal.MetalButtonUI by default.)

  installUI(JComponent c)

Installs this ComponentUI on the specified component. This normally adds listeners to the component and/or its model(s), to notify the UI delegate when changes in state occur that require a view update.

  uninstallUI(JComponent c)

Removes this ComponentUI and any listeners added in installUI() from the specified component and/or its model(s).

  update(Graphics g, JComponent c)

If the component is opaque this should paint its background and then call paint(Graphics g, JComponent c).

  paint(Graphics g, JComponent c)

Gets all information it needs from the component and possibly its model(s) to render it correctly.

  getPreferredSize(JComponent c)

Return the preferred size for the specified component based on this ComponentUI.

  getMinimumSize(JComponent c)

Return the minimum size for the specified component based on this ComponentUI.

  getMaximumSize(JComponent c)

Return the maximum size for the specified component based on this ComponentUI.

To enforce the use of a specific UI delegate we can call a component's setUI() method (note that setUI() is declared protected in JComponent because it only makes sense in terms of a JComponent subclass):

    JButton m_button = new JButton();



Most UI delegates are constructed such that they know about a component and its model(s) only while performing painting and other view-controller tasks. Swing normally avoids associating UI delegates on a per-component basis (hence the static instance). However, nothing stops us from assigning our own as the code above demonstrates.

Note: The JComponent class defines methods for assigning UI delegates because the method declarations required do not involve component-specific code. However, this is not possible with data models because there is no base interface that all models can be traced back to (i.e. there is no base class such as ComponentUI for Swing models). For this reason methods to assign models are defined in subclasses of JComponent where necessary.

1.4.2 Pluggable look-and-feel

Swing includes several sets of UI delegates. Each set contains ComponentUI implementations for most Swing components and we call each of these sets a look-and-feel or a pluggable look-and-feel (PLAF) implementation. The javax.swing.plaf package consists of abstract classes derived from ComponentUI, and the classes in the javax.swing.plaf.basic package extend these abstract classes to implement the Basic look-and-feel. This is the set of UI delegates that all other look-and-feel classes are expected to use as a base for building off of. (Note that the Basic look-and-feel cannot be used on its own, as BasicLookAndFeel is an abstract class.) There are three pluggable look-and-feel implemenations derived from the Basic look-and-feel:

Windows: com.sun.java.swing.plaf.windows.WindowsLookAndFeel

CDE\Motif: com.sun.java.swing.plaf.motif.MotifLookAndFeel

Metal (default): javax.swing.plaf.metal.MetalLookAndFeel

There is also a MacLookAndFeel for simulating Macintosh user interfaces, but this does not ship with Java 2 and must be downloaded separately. The Windows and Macintosh pluggable look-and-feel libraries are only supported on the corresponding platform.[2]

The multiplexing look-and-feel, javax.swing.plaf.multi.MultiLookAndFeel, extends all the abstract classes in javax.swing.plaf. It is designed to allow combinations of look-and-feels to be used simultaneously and is intended for, but not limited to, use with Accessibility look-and-feels. The job of each multiplexing UI delegate is to manage each of its child UI delegates.

Each look-and-feel package contains a class derived from the abstract class javax.swing.LookAndFeel: BasicLookAndFeel, MetalLookAndFeel, WindowsLookAndFeel, etc. These are the central points of access to each look-and-feel package. We use them when changing the current look-and-feel, and the UIManager class (used to manage installed look-and-feels) uses them to access the current look-and-feel's UIDefaults table (which contains, among other things, UI delegate class names for that look-and-feel corresponding to each Swing component). To change the current look-and-feel of an application we can simply call the UIManager's setLookAndFeel() method, passing it the fully qualified name of the LookAndFeel to use. The following code can be used to accomplish this at run-time:

  try {





  catch (Exception  e) {

    System.err.println("Could not load LookAndFeel");


SwingUtilities.updateComponentTreeUI() informs all children of the specified component that the look-and-feel has changed and they need to discard their UI delegate in exchange for a different one of the specified type.

1.4.3    Where are the UI delegates?

We've discussed ComponentUI, and the packages LookAndFeel implementations reside in, but we haven't really mentioned anything about the specific UI delegate classes derived from ComponentUI. Each abstract class in the javax.swing.plaf package extends ComponentUI and corresponds to a specific Swing component. The name of each class follows the general scheme of class name (without the "J" prefix) plus a "UI" suffix. For instance LabelUI extends ComponentUI and is the base delegate used for JLabels.

These classes are extended by concrete implementations such as those in the basic and multi packages. The names of these subclasses follow the general scheme of look-and-feel name prefix added to the superclass name. For instance, BasicLabelUI and MultiLabelUI both extend LabelUI and reside in the basic and multi packages respectively. Figure 1.7 illustrates the LabelUI hierarchy.

Figure 1.7 LabelUI hierarchy

<<file figure1-7.gif>>

Most look-and-feel implementations are expected to extend the concrete classes defined in the basic package, or use them directly. The Metal, Motif, and Windows UI delegates are built on top of Basic versions. The Multi look-and-feel, however, is unique in that each implementation does not extend from Basic, and is merely a shell allowing an arbitrary number of UI delegates to be installed on a given component.

Figure 1.7 should emphasize the fact that Swing supplies a very large number of UI delegate classes. If we were to create an entire pluggable look-and-feel implementation, it is evident that some serious time and effort would be involved. In chapter 21 we will learn all about this process, as well as how to modify and work with the existing look-and-feels.

Chamond Liu, "Smalltalk, Objects, and Design" Manning Publications Co. 1996.

There are some simple ways to get around this but it wouldn't be wise of us to publish them here.

We do not detail the complete functionality and construction of any UI delegate classes in this book. The only reference available at the time of this writing with coverage of the Basic UI delegates is Manning's "Java Foundation Classes: Swing Reference."

[ Return to Swing (Book) ]

Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color

[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

[ More in News Section ]

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy