Well, because all the code that actually catches the mouse event and shows the tooltip is in ToolTipManager
(the instance of this class is singleton, unique in the application), and ToolTipManager
always calls JComponent.createToolTip()
method on component when determining what tooltip to show. So if you want to use your own tooltip you have to override this method and write something like this:
JButton btn = new JButton("SAVE"){
public JToolTip createToolTip() {
JToolTip tip_for_button = new JToolTip(){
public String getTipText() {
return "blah blah";
}
};
tip_for_button.setComponent(this);
return tip_for_button;
}
};
btn.setToolTipText("notnull");
setToolTipText
is mandatory, or the tooltip will not show, and the text passed to it is always set to the tooltip created by Component, so if you want the immutable text, you override JToolTip.getTipText()
.