Amis du swing,
je cherche à définir un tooltiptext pour chaque noeud de mon arbre. Cependant je ne comprends pourquoi celui-ci n'apparait pas...
je suis parti d'une source trouvée sur javareference.com qui semble fonctionner correctement... mais pas chez moi !
Je définis ma classe MyTreeCellRenderer, elle étend un JLabel que je compte modifier pour redéfinir le comportement du tooltip et, bien entendu, elle implémente TreeCellRenderer.
Voici ma classe :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 
public class MyTreeCellRenderer extends JLabel implements TreeCellRenderer {
 
	private static final long serialVersionUID = 6895620207885494809L;
 
	private static final String ICON_CLOSED_FOLDER = "./imgs/7.png";
 
	private static final String ICON_OPENED_FOLDER = "./imgs/3.png";
 
	private static final int SIDE_BTN = 32;
 
	private static final ImageIcon imgOpenFolder = new ImageIcon(new ImageIcon(ICON_OPENED_FOLDER).getImage().getScaledInstance(SIDE_BTN,
			SIDE_BTN, Image.SCALE_SMOOTH));
 
	private static final ImageIcon imgClosedFolder = new ImageIcon(
			new ImageIcon(ICON_CLOSED_FOLDER).getImage().getScaledInstance(
					SIDE_BTN, SIDE_BTN, Image.SCALE_SMOOTH));
 
	/** Color to use for the background when selected. */
	protected Color SelectedBackgroundColor = Color.BLUE;
 
	protected boolean selected;
 
	protected int row;
 
	protected Theme currentTheme;
 
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		DefaultMutableTreeNode node = null;
		String nodeName = "";
		node = (DefaultMutableTreeNode) value;
		if (node.getUserObject() instanceof Theme) {
			currentTheme = (Theme) node.getUserObject();
			nodeName = currentTheme.getLabelTheme();
		} else {
			nodeName = node.getUserObject().toString();
		}
 
		setText(nodeName); // set the text
		if (expanded || selected) {
			setIcon(imgOpenFolder);
		} else {
			setIcon(imgClosedFolder);
		}
 
		if (selected)
			setForeground(Color.WHITE);
		else
			setForeground(Color.BLACK);
 
		/* Update the selected flag for the next paint. */
		this.selected = selected;
		this.row = row;
		this.setToolTipText(nodeName);
		// And finally, return the properly configured JLabel.
		return this;
	}
 
	/**
         * paint is subclassed to draw the background correctly. JLabel currently
         * does not allow backgrounds other than white, and it will also fill behind
         * the icon. Something that isn't desirable.
         */
	public void paint(Graphics g) {
		Color bColor;
		Icon currentI = getIcon();
 
		if (selected)
			bColor = SelectedBackgroundColor;
		else
			bColor = Color.WHITE;
		g.setColor(bColor);
		if (currentI != null && getText() != null) {
			int offset = (currentI.getIconWidth() + getIconTextGap());
 
			g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
		} else
			g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
		super.paint(g);
	}
 
	public Color getSelectedBackgroundColor() {
		return this.SelectedBackgroundColor;
	}
 
	public void setSelectedBackgroundColor(Color selectedBackgroundColor) {
		SelectedBackgroundColor = selectedBackgroundColor;
	}
 
	public int getRow() {
		return row;
	}
 
	/**
         * This method is overriden from JLabel to create and return the
         * ImageToolTip,
         */
	@Override
	public JToolTip createToolTip() {
		// create the ImageToolTip by passing the
		// image to appear on it
		if (currentTheme != null)
			return new ImageToolTip(currentTheme.getGenericPictoTheme()
					.getImage());
 
		// should never get here
		return null;
	}
 
	/**
         * This class extends JToolTip and set the UI to ImageToolTipUI.
         * 
         * @author Rahul Sapkal(rahul@javareference.com)
         */
	public class ImageToolTip extends JToolTip {
		/**
                 * 
                 */
		private static final long serialVersionUID = 6453234229568220331L;
 
		public ImageToolTip(Image image) {
			setUI(new ImageToolTipUI(image));
		}
	}
 
	/**
         * This class extends MetalToolTipUI and provides customizes it to draw a
         * given image on it.
         * 
         * @author Rahul Sapkal(rahul@javareference.com)
         */
	public class ImageToolTipUI extends MetalToolTipUI {
		private Image m_image;
 
		public ImageToolTipUI(Image image) {
			m_image = image;
		}
 
		/**
                 * This method is overriden from the MetalToolTipUI to draw the given
                 * image and text
                 */
		@Override
		public void paint(Graphics g, JComponent c) {
			FontMetrics metrics = c.getFontMetrics(g.getFont());
			// Dimension size = c.getSize();
			// g.setColor(c.getBackground());
			// g.fillRect(0, 0, size.width, size.height);
			g.setColor(Tools.string2Color(currentTheme.getColorTheme()));
 
			g.drawString(((JToolTip) c).getTipText(), 3, 15);
 
			g.drawImage(m_image, 3, metrics.getHeight() + 3, c);
		}
 
		/**
                 * This method is overriden from the MetalToolTipUI to return the
                 * appropiate preferred size to size the ToolTip to show both the text
                 * and image.
                 */
		@Override
		public Dimension getPreferredSize(JComponent c) {
			FontMetrics metrics = c.getFontMetrics(c.getFont());
			String tipText = ((JToolTip) c).getTipText();
			if (tipText == null) {
				tipText = "";
			}
 
			int width = SwingUtilities.computeStringWidth(metrics, tipText);
			int height = metrics.getHeight() + m_image.getHeight(c) + 6;
 
			if (width < m_image.getWidth(c)) {
				width = m_image.getWidth(c);
			}
 
			return new Dimension(width, height);
		}
	}
 
}
J'aurais besoin de vos lumières pour comprendre la raison pour laquelle le tooltip n'apparaît pas !
Merci d'avance !