How to put icon in tooltip?

1

I'm trying to print a text in a tooltip, and in the end, put an icon, however I'm not getting it, I tried doing with html.

The problem is that the image comes "broken", does not appear.

Whatwouldbetherightwaytodothis?

importjava.awt.Dimension;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassToolTipextendsJFrame{privateJTextFieldjTextField=newJTextField();publicToolTip(){add(tela());setSize(300,300);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}privateJComponenttela(){JPanelpainel=newJPanel();painel.add(jTextField);jTextField.setPreferredSize(newDimension(150,20));jTextField.setToolTipText("Nome do campo: " + personalizaToolTip());
        return painel;
    }

    private String personalizaToolTip() {
        //JLabel label = new JLabel();
        //label.setIcon(new ImageIcon(getClass().getResource("/imagens/testeIcon.png")));

        String texto = "<html><body> o campo esta desabilitado.. <img src=\"/imagens/testeIcon.png\"/> </body></html>";
        return texto;
    }

    public static void main(String[] args) {
        ToolTip tp = new ToolTip();
    }
}
    
asked by anonymous 11.10.2017 / 00:39

1 answer

3

Your code works, the problem is you add text outside the html tags, this breaks the tooltip parse.

Change as below:

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JTooltipImageTest extends JFrame {

    private JTextField jTextField = new JTextField();

    public JTooltipImageTest() {
        add(tela());
        setSize(300, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent tela() {
        JPanel painel = new JPanel();
        painel.add(jTextField);
        jTextField.setPreferredSize(new Dimension(150, 20));
        jTextField.setToolTipText(personalizaToolTip("Nome do campo: "));
        return painel;
    }

    private String personalizaToolTip(String text) {


       return "<html><body>" + text + " o campo esta desabilitado.. <img src='" + getClass().getResource("/imagens/testeIcon.png") + "' /></body></html>";
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> JTooltipImageTest tp = new JTooltipImageTest());
    }
}

Notice that I just concatenated your text inside the string, so that it succeeds in the <html> tag, as this should be the first thing in the string for rendering to work correctly.

See a test with an image:

Iwanttodrawattentiontothefactthatthecodedoesnotstartin event-dispatch-thread . Make this a habit, even if it's just a simple example to post here and always start the application within this Thread.

If you have questions about the importance of this, in this answer further explains the reason for this and any issues you may have to occur. This other answer shows you some ways to start the application within this Thread.

    
11.10.2017 / 01:34