开发者

Disable tooltip for disabled buttons

开发者 https://www.devze.com 2023-03-15 08:15 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号