hemanth kumar
hemanth kumar

Reputation: 3078

Disable tooltip for disabled buttons

I am working 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.

Upvotes: 2

Views: 4589

Answers (3)

piotrpo
piotrpo

Reputation: 12636

You have to remove tooltip text.

You can also create your own class with overriden methods for enable/disable and doing it automatically.

Upvotes: 1

Boris Pavlović
Boris Pavlović

Reputation: 64632

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.

Upvotes: 2

SJuan76
SJuan76

Reputation: 24875

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.

Upvotes: 5

Related Questions