| 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 
              Modify GridBagPanelV4 
                so that the user can select one of the available L&Fs from 
                a JList or dropdown menu from a menubar.
 
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 |