How to put icon in tooltip?

Asked

Viewed 276 times

1

I’m trying to print a text on a tooltip, and at the end, put an icon, however I’m not getting it, I tried to do with html.

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

inserir a descrição da imagem aqui

What would be the right way to do it?

import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ToolTip extends JFrame {

    private JTextField jTextField = new JTextField();

    public ToolTip() {
        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("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();
    }
}
  • You can put the icon image there in the question?

  • @Victorstafusa I edited !

  • Okay, that’s not really what I wanted, but it also helps a lot. What I was saying is, put the image that you want to appear, the "/imagens/testeIcon.png".

  • @Victorstafusa is just a little blue circle, just to test.

  • You need to create a custom tooltip, so ai will not add icone.

  • @Articuno can give me an example, if possible ?

Show 1 more comment

1 answer

3


Your code works, the problem is you add text outside the html tags, it 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());
    }
}

Note that I just concatenated your text into the string so that it succeeds the tag <html>, for this must be the first thing in the string for the rendering to work correctly.

See a test with an image:

inserir a descrição da imagem aqui

I want to draw attention to the fact that the code is not started on . Make it a habit, even if it is a mere example to post here and always start the application within this Thread.

If you have any doubts about the importance of that, in this answer better explains the reason for this and any problems that may occur. This other answer shows some ways to start the application within this thread.

  • 1

    @Jeffersonquesado betrayed by copy&paste, corrected,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.