yongsing
asked on
Set disabled text color on JButton
I have a JButton with HTML text:
JButton button = new JButton("<html><center>Man age<br>Use rs</center ></html>") ;
When I disabled the button, the text still appear in black. How do I make the disabled text in gray (as it should appear when it's not in HTML)? This is what I currently do:
button.setText("<html><cen ter><font color=\"666666\">Manage<br >Users</fo nt></cente r></html>" );
button.setEnabled(false);
I don't really like this way of doing. Is there a better way?
JButton button = new JButton("<html><center>Man
When I disabled the button, the text still appear in black. How do I make the disabled text in gray (as it should appear when it's not in HTML)? This is what I currently do:
button.setText("<html><cen
button.setEnabled(false);
I don't really like this way of doing. Is there a better way?
Try the setVisible() method and see what happens...
ASKER
Calling setVisible(false) would have made the whole button disappear.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Extend JButton and override setEnabled(). Include your button-colouring code in the overriding method.
Regards,
Mike
mjsmith99@hotmaildotyoukno wwhat
Regards,
Mike
mjsmith99@hotmaildotyoukno
Override setEnabled and change the HTML button text:
public void setEnabled(boolean flag)
{
if (flag)
{
setText("<html><center><fo nt color=\"666666\">Manage<br >Users</fo nt></cente r></html>" );
}
else
{
setText("<html><center><fo nt color=\"222222\">Manage<br >Users</fo nt></cente r></html>" );
}
super.setEnabled(flag);
repaint();
}
public void setEnabled(boolean flag)
{
if (flag)
{
setText("<html><center><fo
}
else
{
setText("<html><center><fo
}
super.setEnabled(flag);
repaint();
}
ASKER
objects, that was exactly what I had done. I am actually hoping for a less complicated method.
Not sure there is a simpler way.
As you're using HTML for specifying the button text then that is what is being displayed regardless of whether it's enabled or disabled.
As you're using HTML for specifying the button text then that is what is being displayed regardless of whether it's enabled or disabled.
ASKER
Thanks. It seems that there's no other way of doing it.
I created a class that extends JButton and I provide the Enabled and Disabled text with an overridden setEnabled(boolean) method. This way I only have to specify the text in one place and the text changes based on the setEnabled parameter value.
Example usage
JButton button=new TextChangeButton(
"<html><center>Manage<br>U sers</cent er></html> ",
"<html><center><font color=\"666666\">Manage<br >Users</fo nt></cente r></html>" );
...
button.setEnabled(true); // Show manage users in black
button.setEnabled(false); // Show manage users in grey
Example usage
JButton button=new TextChangeButton(
"<html><center>Manage<br>U
"<html><center><font color=\"666666\">Manage<br
...
button.setEnabled(true); // Show manage users in black
button.setEnabled(false); // Show manage users in grey
import javax.swing.JButton;
/**
* Class to change the text of a JButton when the button is enabled or disabled.
* In the case of HTML text, this class can be used to "Gray out" the button if
* it is disabled and reset when the button is enabled.
*
* @author mlempert
*/
public class TextChangeButton extends JButton {
private String enabledText = null;
private String disabledText = null;
/**
* Constructor
* @param enabledText Enabled text
*/
public TextChangeButton(String enabledText) {
super(enabledText);
this.enabledText = enabledText;
}
/**
* Constructor
* @param enabledText Enabled text
* @param disabledText Disabled text
*/
public TextChangeButton(String enabledText, String disabledText) {
this(enabledText);
this.disabledText = disabledText;
}
/**
* Set the button text and the enabled text
*
* @param enabledText Enabled text
*/
public void setText(String enabledText) {
this.enabledText = enabledText;
super.setText(enabledText);
}
/**
* Set the disabled text
*
* @param disabledText
*/
public void setDisabledText(String disabledText) {
this.disabledText = disabledText;
}
/*
* @see javax.swing.AbstractButton#setEnabled(boolean)
*/
public void setEnabled(boolean b) {
super.setEnabled(b);
if (disabledText != null)
super.setText(b ? this.enabledText : this.disabledText);
}
/**
* Show the enabled text
*/
public void showEnabledText(){
super.setText(this.enabledText);
}
/**
* Show the disabled text
*/
public void showDisabledText(){
if (disabledText != null)
super.setText(this.disabledText);
}
}
ASKER
Good effort, mlempert. But unfortunately you are late by almost 6 years, and this question is no longer relevant to me now. :)
I understand. I thought that it might help someone else who might be searching for a similar situation. I ran across this on an internet search for something else.