Adapt ToolTip code to a jTextField already created

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 where to apply this ToolTip .

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

    
asked by anonymous 13.11.2014 / 15:21

1 answer

2

I think it's much simpler to create a Tooltip with HTML (instead of JToolTip ). Multiple components support HTML, test with 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 . You simply set the value as HTML in the setToolTipText method. 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>";


Onlyaveryuglyexampletoillustrate.

JusthaveabasicknowledgeofHTMLandCSS.Idonothaveinformationonhowplatformsupportgoes,thatis,whetheritacceptsnewerCSSandHTMLproperties.Butregardlessofthat,youcandoalot,evenwithjustthebasics.

Ifyoudonotwanttosetone-by-one,youcancreateamethodthatreturnsatooltipstylizedbydefault,containingStringtoinsertintomethodsetToolTipText

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

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


A note : Colors must contain the complete Hexadecimal code to function. Setting #fff instead of #ffffff will not 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);
    }
}
    
19.01.2015 / 06:24