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?