JaVa
   

Using Tool Tips

A nice way to inform the user of what options do in your games is to apply tool tips as much as you can. A tool tip is simply a string of text that appears above (over) an object when the mouse is hovered over it for a predetermined amount of time. Let's look at a simple example of this now as we load in four different images and assign a tool tip to each of the four images:

Code Listing 21: Using tool tips
import java.awt.*;
import javax.swing.*;
public class TooltipExample extends JFrame
{
 public static void main(String[] argv)
 {
 TooltipExample mainApp = new TooltipExample();
 }
 public TooltipExample()
 {
 super("Tool Tip Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 JLabel logo = new JLabel(new ImageIcon("wordwarelogo.jpg"));
 logo.setLocation(10, 10);
 logo.setSize(logo.getPreferredSize());
 JLabel redcircle = new JLabel(new ImageIcon("red.gif"));
 redcircle.setLocation(10, 160);
 redcircle.setSize(redcircle.getPreferredSize());
 JLabel bluecircle = new JLabel(new ImageIcon("blue.gif"));
 bluecircle.setLocation(170, 10);
 bluecircle.setSize(bluecircle.getPreferredSize());
 JLabel yellowcircle = new JLabel(new
 ImageIcon("yellow.gif"));
 yellowcircle.setLocation(170, 160);
 yellowcircle.setSize(yellowcircle.getPreferredSize());
 // Add the tool tips...
 logo.setToolTipText("This is the Wordware Publishing Inc.
 Logo");
 redcircle.setToolTipText("A Red Circle");
 bluecircle.setToolTipText("A Blue Circle");
 yellowcircle.setToolTipText("A Yellow Circle");
 getContentPane().add(logo);
 getContentPane().add(redcircle);
 getContentPane().add(bluecircle);
 getContentPane().add(yellowcircle);
 setVisible(true);
 }
 JLabel logo;
 JLabel redcircle, bluecircle, yellowcircle;
}


Java End example

Here is how the example looks when we execute it and hover the mouse over the red circle image:

Java ScreenShot
Screenshot: Using tool tips

As you can see from the code, it is very easy to add tool tips. They can be added to most GUI objects in Java. For example, here is the line of code that we have used to add the tool tip to the Wordware logo:

logo.setToolTipText("This is the Wordware Publishing Inc. Logo");


Hope this is useful!

Andrew + Glenn

JaVa
   
Comments