Solved

Set disabled text color on JButton

Posted on 2002-03-21
11
2,713 Views
Last Modified: 2010-08-05
I have a JButton with HTML text:

JButton button = new JButton("<html><center>Manage<br>Users</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><center><font color=\"666666\">Manage<br>Users</font></center></html>");
button.setEnabled(false);

I don't really like this way of doing. Is there a better way?
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 1

Expert Comment

by:harvk
ID: 6884960
Try the setVisible() method and see what happens...

0
 
LVL 9

Author Comment

by:yongsing
ID: 6884999
Calling setVisible(false) would have made the whole button disappear.
0
 
LVL 1

Accepted Solution

by:
saurav17 earned 50 total points
ID: 6885018


Hi yongsing,

What u have done now is the only solution to that.

If u use setvisible method, the button on whole will disappear.

I'm sorry u have no other options.


Regards,
K.J.S.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:mjsmith99
ID: 6885115
Extend JButton and override setEnabled().  Include your button-colouring code in the overriding method.



Regards,
Mike
mjsmith99@hotmaildotyouknowwhat
0
 
LVL 92

Expert Comment

by:objects
ID: 6886948
Override setEnabled and change the HTML button text:

public void setEnabled(boolean flag)
{
  if (flag)
  {
     setText("<html><center><font color=\"666666\">Manage<br>Users</font></center></html>");
  }
  else
  {
     setText("<html><center><font color=\"222222\">Manage<br>Users</font></center></html>");
  }
  super.setEnabled(flag);
  repaint();
}

0
 
LVL 9

Author Comment

by:yongsing
ID: 6887489
objects, that was exactly what I had done. I am actually hoping for a less complicated method.
0
 
LVL 92

Expert Comment

by:objects
ID: 6887513
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.
0
 
LVL 9

Author Comment

by:yongsing
ID: 6887902
Thanks. It seems that there's no other way of doing it.
0
 
LVL 2

Expert Comment

by:mlempert
ID: 20671573
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>Users</center></html>",
    "<html><center><font color=\"666666\">Manage<br>Users</font></center></html>");
...
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);
    }
}

Open in new window

0
 
LVL 9

Author Comment

by:yongsing
ID: 20678435
Good effort, mlempert. But unfortunately you are late by almost 6 years, and this question is no longer relevant to me now. :)
0
 
LVL 2

Expert Comment

by:mlempert
ID: 20690839
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.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 65
Java program running SQL query 5 68
Eclipse for Java EE development 2 70
DTD and JAVA versions 1 59
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question