- Colored ToolTips
- Detachable/Non-Detachable Toolbars
- Individualized List ToolTips
- Non-Auto-Resize Table Columns
- Table Header Icons
Individualized List ToolTips
A ToolTip can be assigned to a list component. When the mouse cursor hovers over the component, that ToolTip will appearregardless of what list item the mouse cursor is over. Although assigning a single ToolTip to the list is okay in many situations, there are times when it's preferable to display item-specific ToolTips (see Figure 5).
Each list item in a list component can have its own ToolTip. In this example, the French word for the day of the week appears as a ToolTip.
To create individualized ToolTips, begin by subclassing Swing's JList class. Declare a constructor that takes two arguments: an array of list item Strings and an array of ToolTip Strings. The constructor uses these arrays to create a single Object array, in which each array element consists of an item/ToolTip pair. This Object array becomes the ListModel for the JList subclass component.
The next step is to override JList's getToolTipText method. Whenever the mouse cursor moves over the resulting subclass component, this overridden getToolTipText method will be called to return the ToolTip text. To do its job, getToolTipText must locate the list item over which the mouse cursor is hovering. Then, the appropriate Object element is retrieved from the ListModel and the ToolTip text is retrieved. This text is then returned from getToolTipText and displayed.
Listing 3 presents source code to an IndividualizedListTT application. This source code declares a JList subclass and shows how to achieve individualized list ToolTips. (The GUI in Figure 5 was generated by this application.)
Listing 3 The IndividualizedListTT application source code
// IndividualizedListTT.java import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.event.*; class IndividualizedListTT extends JFrame { IndividualizedListTT (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); String [] items = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; String [] tips = { "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche" }; // Create a TTList component object that displays items and // tips. TTList ttl = new TTList (items, tips); // Highlight the first item in the list. ttl.setSelectedIndex (0); // Create a JScrollPane and add the TTList to this container. JScrollPane sp = new JScrollPane (ttl); // Set the JScrollPane's preferred size so that a vertical // scrollbar will be displayed with the JScrollPane. sp.setPreferredSize (new Dimension (100, 75)); // Create a JPanel and add the JScrollPane to the JPanel so that // the JScrollPane will be displayed at a more natural size. JPanel p = new JPanel (); p.add (sp); // Add the JScrollPane to the JFrame's content pane. getContentPane ().add (p); // Set the size of the JFrame window. setSize (250, 125); // Show the JFrame window. setVisible (true); } public static void main (String [] args) { new IndividualizedListTT ("Individualized List ToolTips"); } } class TTList extends JList { TTList (String [] data, String [] tips) { // Initialize the JList layer of the TTList object. super (); // Assign a new ListModel to the TTList. setListData (createLinks (data, tips)); // If the TTList displays a ToolTip, we cannot assign individual // ToolTips to TTList items. Therefore, "" is passed to // setToolTipText to cancel the default ToolTip. setToolTipText (""); } public String getToolTipText (MouseEvent e) { // Convert the mouse coordinates where the left mouse button was // pressed to a TTList item index. int index = locationToIndex (e.getPoint ()); // If the left mouse button was clicked and the mouse cursor was // not over a list item, index is set to -1. if (index > -1) { // Extract the ListModel. ListModel lm = (ListModel) getModel (); // Get the ToolTipLink associated with the TTList item // index. ToolTipLink link = (ToolTipLink) lm.getElementAt (index); // Return the ToolTipLink's ToolTip text. return link.getToolTipText (); } else return null; } private Object [] createLinks (String [] data, String [] tips) { // Create an array of references to ToolTipLink objects -- one // reference per TTList item. ToolTipLink [] links = new ToolTipLink [data.length]; // For each TTList item, create a ToolTipLink object and store // its reference in the previously created array. for (int i = 0; i < data.length; i++) links [i] = new ToolTipLink (data [i], tips [i]); return links; } // The following class maintains an association between a TTList // item name and its associated tipText. private class ToolTipLink { private String name; private String tipText; public ToolTipLink (String name, String tipText) { this.name = name; this.tipText = tipText; } public String getToolTipText () { return tipText; } // toString is called behind the scenes by JList's UI to extract // the TTList name for display. public String toString () { return name; } } }