JToolTip With Icon : Tooltip « Swing JFC « Java






JToolTip With Icon

     
//package modrcon;

import java.awt.*;
import javax.swing.plaf.metal.MetalToolTipUI;
import javax.swing.*;

public class JToolTipWithIcon extends JToolTip {
    
    protected ImageIcon icon;

    public JToolTipWithIcon(ImageIcon icon) {
        this.icon = icon ;
        setUI(new IconToolTipUI()) ;
    }

    public JToolTipWithIcon(MetalToolTipUI toolTipUI) {
        setUI(toolTipUI) ;
    }

    private class IconToolTipUI extends MetalToolTipUI {
        @Override
        public void paint(Graphics g, JComponent c) {
            FontMetrics metrics = c.getFontMetrics( c.getFont() ) ;
            Dimension size = c.getSize() ;
            g.setColor( c.getBackground() ) ;
            g.fillRect( 0, 0, size.width, size.height ) ;
            int x = 3 ;
            if(icon != null) {
                icon.paintIcon( c, g, 0, 0 ) ;
                x += icon.getIconWidth() + 1 ;
            }
            g.setColor( c.getForeground() ) ;
            g.drawString( ((JToolTip)c).getTipText(), x, metrics.getHeight() ) ;
        }
        @Override
        public Dimension getPreferredSize(JComponent c) {
            return new Dimension(icon.getIconWidth(), icon.getIconHeight());
        }
    }
}

   
    
    
    
    
  








Related examples in the same category

1.Tooltip SampleTooltip Sample
2.Html tooltipsHtml tooltips
3.Working with Tooltip TextWorking with Tooltip Text
4.MultiLine ToolTip ExampleMultiLine ToolTip Example
5.Colored ToolTip ExampleColored ToolTip Example
6.Disable / enable application tool tips
7.Making Tool Tips Remain Visible
8.Enabling and Disabling Tool Tips
9.Making Tool Tips Appear Immediately
10.Use the default tool tip location
11.Make a tool tips appear immediately
12.Showing an Image in a Tool Tip with HTML
13.Setting a Tool Tip
14.Setting the Location of a Tool Tip
15.By default, the lines are left justified. Center the lines.
16.Set the location of the tool tip such that its nw corner coincides with the bottom center of the button
17.Showing an Image in a ToolTip
18.Modify the behaviour of the default JToolTip
19.Italicize the second line
20.Changing ToolTip background color for Swing Applications
21.ToolTip Location ExampleToolTip Location Example
22.Set a tool tip for swing component