Disable tooltip for disabled buttons
I am 开发者_如何学JAVAworking on a swing gui which have many buttons. I have many actions in which buttons disable and enable at times. I want to set tooltips for only enabled buttons. When the button disables I don't want any tooltip for that button.
I would try extending the Button class, and overloading getTooltip(). Something like:
public class MyButton extends JButton {
public String getTooltip() {
if (this.isEnabled()) {
return super.getTooltip();
}
return null;
}
}
Of course, this depends on Swing using getTooltip to get the info to draw the button; anyway I would try it.
Add an extended JButton
class:
import javax.swing.*;
public class MyButton extends JButton
{
private String toolTip;
@Override
public void setToolTipText(String text)
{
super.setToolTipText(text);
if (null != text) toolTip = text;
}
@Override
public void setEnabled(boolean b)
{
super.setEnabled(b);
super.setToolTipText(b ? toolTip : null);
}
}
and use it instead.
You have to remove tooltip text.
You can also create your own class with overriden methods for enable/disable and doing it automatically.
精彩评论