IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

[SWING]Redéfinition d'un tooltiptext sur un élément de JTree !


Sujet :

Composants Java

  1. #1
    Membre à l'essai
    Inscrit en
    Décembre 2003
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 38
    Points : 22
    Points
    22
    Par défaut [SWING]Redéfinition d'un tooltiptext sur un élément de JTree !
    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 !

  2. #2
    Membre à l'essai
    Inscrit en
    Décembre 2003
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 38
    Points : 22
    Points
    22
    Par défaut
    A noter, qu'après l'appel à
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ToolTipManager.sharedInstance().registerComponent(jTreeThemes);
    ,
    lors de l'initialisation de mon arbre, un tooltiptext apparaît mais il ne correspond pas à ce que je voudrais.
    En effet, j'obtiens le tooltiptext "classique" au lieu d'en avoir un avec une image à l'intérieur. Pour info, je ne peux pas vérifier si le contenu de l'image n'est pas null car le debug ne passe pas ds mes méthodes de redéfinition de tooltiptext..


    A l'aide !

  3. #3
    Membre à l'essai
    Inscrit en
    Décembre 2003
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 38
    Points : 22
    Points
    22
    Par défaut
    Mon problème n'a pas l'air bien inspirant...

  4. #4
    Membre à l'essai
    Inscrit en
    Décembre 2003
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 38
    Points : 22
    Points
    22
    Par défaut
    problème résolu en redéfinissant le getToolTipText(MouseEvent) du JTree :
    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
    public class MyJXTree extends JXTree {
     
    	Theme currentTheme = null;
     
    	String nodeName;
     
    	/**
             * 
             */
    	private static final long serialVersionUID = -4113489050436870860L;
     
    	/**
             * 
             */
    	public MyJXTree() {
    		super();
    	}
     
    	@Override
    	public String getToolTipText(MouseEvent me) {
    		TreePath overPath = this.getPathForLocation(me.getX(), me.getY());
    		if (overPath != null) {
    			DefaultMutableTreeNode overNode = (DefaultMutableTreeNode) overPath
    					.getLastPathComponent();
    			if (overNode.getUserObject() instanceof Theme) {
    				currentTheme = (Theme) overNode.getUserObject();
    				return currentTheme.getLabelTheme();
    			} else {
    				nodeName = overNode.getUserObject().toString();
    				return nodeName;
    			}
    		}
    		return null;
    	}
     
    	/**
             * 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);
    		}
    	}
     
    }
    au cas où ça pourrait servir à d'autres qui voudraient définir un tooltiptext pour chaque élément d'un arbre.
    A noter que cela fonctionne aussi pour n'importe quel composant dérivant de JComponent (ex : JTable, JTabbedPane).
    Bonne fin de journée.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fest Swing NullPointer sur mes éléments
    Par nddvlp dans le forum Tests et Performance
    Réponses: 3
    Dernier message: 23/08/2012, 08h41
  2. Réponses: 2
    Dernier message: 23/10/2005, 19h00
  3. [VB6] COMBO: se positionner sur un élément
    Par taurus dans le forum VB 6 et antérieur
    Réponses: 15
    Dernier message: 18/10/2005, 11h26
  4. Sort sur un élément distinct
    Par Martin lalande dans le forum Langage
    Réponses: 1
    Dernier message: 19/09/2005, 19h05
  5. [JTree] ToolTipText sur chaque node
    Par Stessy dans le forum Composants
    Réponses: 6
    Dernier message: 19/04/2005, 16h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo