- Forward


Customizing Components in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Introduction
Back SMYC Forward
  • It is possible to change the appearance of a GUI by building a complete "widget set"
  • It is also possible to change the appearance of a widget set that uses the Model-View-Controller pattern by writing a complete view
  • We now consider ways to change the appearance of a GUI that don't involve building a complete widget set or view
Themes
Back SMYC Forward
  • Defined:
    • The fonts and colors used by GUI components
  • The Java Look-and-Feel:
    • Commonly called "Metal"
    • Supports themes
Themes (cont.)
Back SMYC Forward

Creating a Theme

  1. Create a class that extends DefaultMetalTheme
  2. Override the desired accessor methods
javaexamples/lookandfeel/JMUTheme.java
 
Themes (cont.)
Back SMYC Forward

Using a Theme

  1. Construct an instance of the theme
  2. Call the static setCurrentTheme method in the MetalLookAndFeel class
javaexamples/lookandfeel/ThemeDemo.java
 
Client Properties
Back SMYC Forward
  • Defined:
    • The attributes of a view
  • Implemented:
    • Key/value pairs
    • When a client property changes a PropertyChangeEvent is dispatched to all registered PropertyChangeListeners
Client Properties (cont.)
Back SMYC Forward
  • JTree.lineStyle:
    • Determines how edges in the graph are shown ("Horizontal", "Angled" or "None")
  • JScrollBar.isFreeStanding:
    • Determines how borders are drawn (Boolean.FALSE indicates that sides will have an etched borders, Boolean.TRUE indicates that only the top and left edges will have etched borders)
  • JSlider.isFilled:
    • Determines whether the lower portion of a slider should be filled (Boolean.TRUE) or not (Boolean.FALSE)
  • JToolBar.isRollover:
    • If Boolean.TRUE etched border is displayed only when the mouse is within its bounds and no border otherwise. If Boolean.FALSE, an etched border is always displayed
  • JInternalFrame.isPalette:
    • If Boolean.TRUE, a thin border is used; if Boolean.FALSE, a thicker border is used
Swapping Views between Components
Back SMYC Forward
  • Some Details:
    • When a JComponent creates a view, its updateUI method is called
    • Normally, this method calls two static methods in the LookAndFeel class that are used to "install" the view, the installBorder and installColorsAndFont methods
  • An Implication:
    • By overriding the updateUI method it is possible to swap borders, colors, and fonts between components
Swapping Views between Components (cont.)
Back SMYC Forward

Make a JTextArea look like a JLabel

javaexamples/gui/WrappingLabel.java
 
Swapping Views between Components (cont.)
Back SMYC Forward

What exists and what can be changed?

javaexamples/lookandfeel/ShowUIDefaults.java
 
Adding Capabilities to a View
Back SMYC Forward
  • Multiplexing:
    • Creating a "composite" view from different views
  • In Java:
    • The UIManager class has a static method named addAuxiliaryLookAndFeel that allows you to add to a multiplexing view
    • You must then create a view (that typically extends ComponentUI) and an extension of LookAndFeel that adds the view to the "defaults"
Adding Capabilities to a View
Back SMYC Forward

An Example

javaexamples/lookandfeel/AudioButtonUI.java
 
javaexamples/lookandfeel/AudioLookAndFeel.java
 
javaexamples/lookandfeel/AudioLFDemo.java
 
Building a View for One (or a Few) Widgets
Back SMYC Forward

An Example

javaexamples/lookandfeel/JMUSliderUI.java
 
javaexamples/lookandfeel/JMULookAndFeel.java
 
javaexamples/lookandfeel/JMULAFDemo.java
 
There's Always More to Learn
Back -