Home > Articles > Programming > Java

A Handful of Tips for Swing Programs

  • Print
  • + Share This
Java expert Geoff Friesen presents a handful of tips that can enhance your Swing programs. All tips were compiled and tested using SDK 1.3 on a Windows 98 SE platform.
From the author of

Colored ToolTips

Have you ever wanted to change the foreground and/or background colors of your ToolTips but didn't know how to accomplish this task? If so, keep reading.

To change the foreground and background colors for all ToolTips, override the default values assigned to the ToolTip.foreground and ToolTip.background color properties in UIManager's defaults table, as demonstrated by the following code fragment:

// Override the ToolTip.foreground color in Swing's defaults table.

UIManager.put ("ToolTip.foreground", Color.white);

// Override the ToolTip.background color in Swing's defaults table.
UIManager.put ("ToolTip.background", Color.blue);

The UIManager class keeps track of the current look and feel as well as default values for look and feel–specific properties (such as colors, fonts, and so on). UIManager uses a defaults table to maintain these default values. After the preceding code fragment executes, the defaults table contains Color.white as the default ToolTip foreground color value and Color.blue as the default ToolTip background color value.

TIP

The java.awt.Color class declares several color constants in addition to Color.white and Color.blue. Consult the SDK 1.3 documentation on java.awt.Color for a complete list of these constants.

Figure 1 illustrates the new ToolTip color scheme.

Figure 1

A GUI's appearance can be enhanced by changing the colors of its ToolTips.

The GUI in Figure 1 was generated by an application called ColoredTT. Listing 1 presents the source code for this application. (Note: You can download the files for the sample applications in this article by clicking here.)

Listing 1  The ColoredTT application source code

// ColoredTT.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class ColoredTT extends JFrame
{
   ColoredTT (String title)
   {
      // Pass the title argument to the JFrame superclass so that the
      // contents of title appear in the JFrame's title bar.

      super (title);

      // Exit the application by calling System.exit when the user
      // either selects Close from the System menu or clicks the X
      // button.

      setDefaultCloseOperation (EXIT_ON_CLOSE);

      // Override the ToolTip.foreground color in Swing's defaults
      // table.

      UIManager.put ("ToolTip.foreground", Color.white);

      // Override the ToolTip.background color in Swing's defaults
      // table.

      UIManager.put ("ToolTip.background", Color.blue);

      // Create a JButton component.

      JButton btn = new JButton ("Move mouse cursor over me.");

      // Assign a ToolTip to this component.

      btn.setToolTipText ("ToolTip appears white on blue.");

      // Create a JPanel container and add the button to this
      // container.  The resulting button can be displayed at a
      // more natural size.

      JPanel jp = new JPanel ();
      jp.add (btn);

      // Add the JPanel container to the JFrame's content pane.

      getContentPane ().add (jp);

      // Set the size of the JFrame window.

      setSize (300, 80);

      // Show the JFrame window.

      setVisible (true);
   }

   public static void main (String [] args)
   {
      new ColoredTT ("Colored ToolTips");
  }
}
  • + Share This
  • 🔖 Save To Your Account