Adapt Tooltip code to an already created jTextField

Asked

Viewed 500 times

1

I found this code that works well for the button:

public class CustomJToolTipTest {

    private JFrame frame;

    public CustomJToolTipTest() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CustomJToolTipTest();
            }
        });
    }

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);


        JButton button = new JButton("button") {
            //override the JButtons createToolTip method
            @Override
            public JToolTip createToolTip() {
                return (new CustomJToolTip(this));
            }
        };
        button.setToolTipText("I am a button with custom tooltip");

        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }
}

class CustomJToolTip extends JToolTip {

    public CustomJToolTip(JComponent component) {
        super();
        setComponent(component);
        setBackground(Color.black);
        setForeground(Color.red);
    }
}

There is a frame with the fields to apply this ToolTip.

How to adapt code to a field instead of a button?

  • This then arrow a custom tooltip with black color and red letters. You really need a custom tooltip (with different colors or sizes, degrade, images, animations, whatever) or just put a standard tooltip with a text to be defined by you is already great?

  • For me it’s enough to change the background and letters color but I’ve seen several examples out there that are even two or three lines of code but in mine it doesn’t work :S

  • Yes, me too. I just wanted to know if you just need the text or need wrinkles too.

  • Just text. Can you change the color of the background and text only? If so, how? Thank you

1 answer

2

I think it’s much simpler to create a Tooltip with HTML (instead of JToolTip). Multiple components support HTML, test with one JLabel:

myJlabel.setText(
   "<html><p style='color:#ffffff;background:#000000'>Preto e Branco</p></html>"
);

The same thing can be done in tooltip of a JTextField. Just set the value to HTML in the method setToolTipText. A very simple example:

String tooltipHtml =
   "<html><p style='background:#2ecc71;border:none;color:#ffffff;padding: 6px;width:200px'>"
   + "UAU! Eu sou uma Tooltip estilosa...</p></html>";

imagem
Just one example ugly to illustrate.

Just have a basic knowledge of HTML and CSS. I have no information about how the platform support is going, that is, if it accepts newer CSS and HTML properties. But regardless, you can do a lot, even if you only have the basics.

If you don’t want to set a-a-a, you can create a method that returns a tooltip stylized by default, containing the String to insert into the method setToolTipText

private String getDefaultToolTip(String message){
   return "<html><p style='background:#000000;color:#ffffff'>" + message + "</p></html>"; 
}

// E em algum lugar do código...
textField.setToolTipText(getDefaultToolTip("Olá"));


A remark: Colors must contain the full Hexadecimal code to work. Set #fff instead of #ffffff won’t work.


test code

import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class ToolTip extends JFrame {

    public ToolTip() throws HeadlessException {
        super("Tooltip");
        init();
    }

    private void init(){
        setSize(300, 100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);

        String tooltipHtml =
        "<html><p style='background:#2ecc71;color:#ffffff;padding: 6px;width:200px'>"
                + "UAU! Eu sou uma Tooltip estilosa...</p></html>";

        JTextField tfTooltip = new JTextField("Eu tenho Tooltip");
        tfTooltip.setBounds(10, 15, 260, 35);
        tfTooltip.setToolTipText(tooltipHtml);
        getContentPane().add(tfTooltip);
    }

    public static void main(String[] args) {
        new ToolTip().setVisible(true);
    }
}

Browser other questions tagged

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