Java Institute DreamsCity 2000
Welcome to DreamsCity
Return to Java Institute

Customizing JToolTips

These tips were developed using Java(tm) 2 SDK, Standard Edition, v 1.3. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CUSTOMIZING JTOOLTIPS JToolTip is a Swing class that you use to provide a tip for a Swing component. When the mouse cursor is moved over the component, a short text message is displayed describing the function of the component. It's easy to set a tip for a component; you just say: comp.setToolTipText("tip text"); Let's look at a couple of ways of customizing tool tips, in the context of the following application: import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; // a customized label that displays a color fade image class ColorLabel extends JLabel { private static final int WIDTH = 100; // label width private static final int HEIGHT = 100; // label height private static final int SZ = 20; // size of tip area private static Image img; // generated image for label private static ImageIcon icon; // ImageIcon for the image // generate a color fade image // adapted from 1.3 java/awt/image/MemoryImageSource.java static { // generate the pixel array int pixels[] = new int[WIDTH * HEIGHT]; int index = 0; for (int y = 0; y < HEIGHT; y++) { int red = (y * 255) / (HEIGHT - 1); for (int x = 0; x < WIDTH; x++) { int blue = (x * 255) / (WIDTH - 1); pixels[index++] = (255 << 24) | (red << 16) | blue; } } // generate the actual image from the pixels img = Toolkit.getDefaultToolkit().createImage( new MemoryImageSource(WIDTH, HEIGHT, pixels, 0, WIDTH)); icon = new ImageIcon(img); } // an inner class, objects of which represent one // customized tooltip with bounding box and text specified static class Tip { Rectangle rect; String text; Tip(Rectangle r, String t) { rect = r; text = t; } }; // the list of custom tooltips static Tip tips[] = { new Tip(new Rectangle(0, 0, SZ, SZ), "Black Part"), new Tip(new Rectangle(WIDTH - SZ, 0, SZ, SZ), "Blue Part"), new Tip(new Rectangle(0, HEIGHT - SZ, SZ, SZ), "Red Part"), new Tip(new Rectangle(WIDTH - SZ, HEIGHT - SZ, SZ, SZ), "Pink Part"), }; // constructor for ColorLabel // set the label image and the default tooltip text public ColorLabel() { super(icon); setToolTipText("Color Fade Example"); } // override of JComponent.getToolTipText to support // custom tooltips based on the mouse position public String getToolTipText(MouseEvent e) { // get mouse position Point p = e.getPoint(); // see if it's in any of the custom tooltip // bounding boxes for (int i = 0; i < tips.length; i++) { if (tips[i].rect.contains(p)) { return tips[i].text; } } // if not, return default return getToolTipText(); } } public class ToolTipDemo { public static void main(String args[]) { // set up the frame and the window closing event handler JFrame frame = new JFrame("ToolTipDemo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // create an Exit button with a customized // tooltip that uses an italicized font JButton button = new JButton("Exit") { public JToolTip createToolTip() { JToolTip t = super.createToolTip(); t.setFont(new Font("TimesRoman", Font.ITALIC, 16)); return t; } }; button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); button.setToolTipText("Terminate the application"); // set up the panel JPanel panel = new JPanel(); panel.add(new ColorLabel()); panel.add(button); // display the frame frame.getContentPane().add(panel); frame.setSize(200, 150); frame.setLocation(300, 200); frame.setVisible(true); } } This program draws a color fade box on the screen. A color fade is a gradual change from one color to another, for example from black to blue across the top of the box. The color fade example is adapted from that found in the comments in java/awt/image/MemoryImageSource.java for JDK 1.3. The color fade is calculated into a pixel array, which is then used to construct the Image object. An ImageIcon is then formed from the image. The ImageIcon is used to set the icon for the JLabel object that represents the box. There's also an Exit button drawn next to the box. The first type of tooltip customization is for the Exit button. The text of the tip is changed to a 16-point italicized Times Roman font. The program does this by overriding JComponent.createToolTip. Notice that the overriding method calls the superclass's createToolTip method to get the tip object; the overriding method then sets the font for the object. The other kind of customization is more sophisticated. If you have an application with a complex GUI component in it, it would be nice to customize tooltips based on the position of the mouse within the component. To do this, you can override JComponent.getToolTipText(MouseEvent). By default, this method simply returns the text that was set with setToolTipText. But you can specify your own version of the method, and obtain the mouse cursor position; you can then return custom text based on the position. The example program above sets a general tip "Color Fade Example" for the color fade box. Then the program calls getToolTipText to get the mouse position. getToolTipText also checks whether the mouse is in any of the four corners of the box. A corner is defined to be 20 x 20 pixels. If the mouse is in one of the corners, a custom tip such as "Blue Part" is displayed. Other types of tooltip customization are possible, for example, you can set a preferred location for the display of a tooltip.

Any comments? email to:
richard@dreamscity.net