Home : Course Map : Chapter 7 : Java : Supplements :
Enhancements to UI Components and Layout: Part 2
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New
We continue here with our discussion of enhancements to the Swing graphical user interface by giving an example of altering the Look & Feel. We finish up with a bried mention of some other visual and functional enhancements techniques.

Look & Feel

In Swing the group of settings for the GUI color scheme, the border designs, etc. is referred to as the Look & Feel. There is a default L&F for a given platform. There are typically some optional ones available. You can design your own custon L&Fs as well.

GridBagAppletV4 shown below is the same as GridBagAppletV4 except for the Look & Feel code displayed in red at the start of the GridBagPanelV4 class. This code first obtains a list describing the L&F available to the JVM and then selects one (hardwired in this case) and installs it.

See the references for further information on Look & Feel manipulation.

 

GridBagLayoutV4
Resources: bluebox.jpg, picButton.jpg, picButtonPressed.jpg, face.gif, redBall.gif, redDot.gif

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;


/**
  * Our third modification to GridBagLayout involves adding
  * icons to buttons and labels.
**/
public class GridBagAppletV4 extends JApplet{

  public void init () {
    Container content_pane = getContentPane ();

    // Create an instance of the GridBagPanel
    GridBagPanelV4 grid_bag_panel = new GridBagPanelV4 (this);

    // And add it to the applet's panel.
    content_pane.add (grid_bag_panel);
  } // init

} // GridBagAppletv4

/**
  * Create a JPanel with 5 components and use GridBagLayout
  * for the overall layout.
**/
class GridBagPanelV4 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints ();

  // The Insets constructor setting are passed as follows:
  // Insets(int top, int left, int bottom, int right)
  static final Insets fInsets = new Insets(2,2,2,2);

  // Need a reference back to the applet to use it for getting images.
  JApplet fApplet;

  GridBagPanelV3 (JApplet applet) {

  GridBagPanelV4 (JApplet applet) {

    // Get the available Look and Feels.
    UIManager.LookAndFeelInfo [] lanf =
      UIManager.getInstalledLookAndFeels ();

    // Print out the list of L&Fs
    for (int i=0; i < lanf.length; i++) {
       System.out.println (i+". L&F installed = " +
lanf[i].getName ());
    }

    // Hard-wired selection of the L&F
    int L_AND_F_Select = 1; // Select one of the Look and Feels

    // Set the chosen L&F
    try {
      UIManager.setLookAndFeel (
          //UIManager.getCrossPlatformLookAndFeelClassName ()
          //new javax.swing.plaf.metal.MetalLookAndFeel ()
          //UIManager.getSystemLookAndFeelClassName()
          lanf [L_AND_F_Select].getClassName ()
        );
    }
    catch (Exception e) {
       System.out.println ("Set L&F error!");
    }


  ... Rest same as GridBagPanelV3 ..

 

More Enhancements

There are a number of other enhancements available to the GUI designer. You can, for example, do custom painting of components if techniques like displaying an icon is not satisfactory. See the Performing Custom Painting - Sun Java Tutorial for more info.

You can also add tool tips to components. These are messages that float above a component when the cursor hovers above it. See How to Use Tool Tips - Sun Java Tutorial.

You can control what is the sequence of components reached via the tab key. See How to Use the Focus Subsystem - Sun Java Tutorial.

In Chapter 23: System Properties we discuss how to access the various local graphics settings in case you want to tune your program's display to harmonize your UI with local platforms.

Exercises

  1. Modify GridBagPanelV4 so that the user can select one of the available L&Fs from a JList or dropdown menu from a menubar.

  2. Combine all of the above techniques in a coherent manner to create an example of a complete and beautiful user interface.

References & Web Resources

Latest update: Mar.8, 2006

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.