KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBBasicComboBoxRenderer


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import javax.swing.plaf.basic.BasicComboBoxRenderer JavaDoc;
5 import java.awt.*;
6
7
8 /**
9  * This class basically allows us to set the render of a JComboBox. More to the point...
10  * This is what we have to use to get text tool tip working on each menu item!
11  *
12  * @author Trudi.
13  */

14
15 public class CBBasicComboBoxRenderer extends BasicComboBoxRenderer JavaDoc
16 {
17     public Object JavaDoc[] text; //TE: array of the values to be used in the combo box and to be used as their tooltips..
18

19     /**
20      * Constructor that basically assigns the text variable.
21      *
22      * @param txt the values that are to be added to the combo box and also used as tooltips respectfully.
23      */

24
25     public CBBasicComboBoxRenderer(Object JavaDoc[] txt)
26     {
27         super();
28         text = txt;
29     }
30
31     /**
32      * Defines the getListCellRendererComponent method of BasicComboBoxRender to add
33      * tool tips and text to combo boxes in the super search dialog.
34      * <p/>
35      * Following copied from interface: javax.swing.ListCellRenderer...
36      * Returns a component that has been configured to display the specified value.
37      * That component's paint method is then called to "render" the cell. If it is
38      * necessary to compute the dimensions of a list because the list cells do not
39      * have a fixed size, this method is called to generate a component on which
40      * getPreferredSize can be invoked.
41      *
42      * @param list - The JList we're painting.
43      * @param value - The value returned by list.getModel().getElementAt(index).
44      * @param index - The cells index.
45      * @param isSelected - True if the specified cell was selected.
46      * @param cellHasFocus - True if the specified cell has the focus.
47      * @return a component whose paint() method will render the specified value.
48      */

49
50     public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus)
51     {
52         if (isSelected)
53         {
54             setBackground(list.getSelectionBackground());
55             setForeground(list.getSelectionForeground());
56
57             if (index > -1)
58             {
59                 list.setToolTipText(value.toString());//(text[index].toString());
60
setText(text[index].toString()); //TE: sets the tool tips and the text of each item in the combo.
61
}
62         }
63         else
64         {
65             setBackground(list.getBackground());
66             setForeground(list.getForeground());
67         }
68         setFont(list.getFont());
69         setText((value == null) ? "" : value.toString());
70         return this;
71     }
72 }
Popular Tags