Reputation: 17454
Java provides a simple method to disable its buttons with button.setEnabled(false);
However doing so will grey out the entire button thus affecting the visibility of the text and images on the disabled button.
Question: Are there any available methods/ways in Java to allow buttons to be disabled yet does not grey out?
Certainly, I am not expecting manual tweaking on the ActionListener
of the buttons to achieve this.
Upvotes: 3
Views: 1858
Reputation: 7910
This is a little bit of work, and may not be the best coding style or solution, but it might satisfy your requirements. I'm going to assume you have an icon for when the button is enabled and another for when it is not.
Basically, create and manage your own, virtual "disabled" state for the button (but don't touch the built-in enabled or disabledIcon methods).
In the code that manages the enabling/disabling, when you disable "virtually", set the regular icon ( setIcon() ) to be your disabled Icon graphic, and set a boolean to reflect the virtual "disabled" state of the button. When you "enable", use setIcon() to return the default Icon, and set the boolean to reflect "enabled".
Then, when the button is clicked, in the ActionListener, you can inspect the boolean and do nothing if the boolean says the virtual state is "disabled".
Upvotes: 0
Reputation: 324098
Companies spend millions of dollars to develop a UI can is common and can be used by all users.
How is the user suppose to know that the button is disabled if there is no visual indication?
I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Why? What is wrong with this approach? It is better than trying to create a custom LAF for all platforms your code might run on.
Anyway (rant finished) you could use a custom ButtonModel:
button.setModel( new DefaultButtonModel()
{
@Override
public boolean isArmed()
{
return false;
}
@Override
public boolean isPressed()
{
return false;
}
});
Should work for all LAF's and the button won't be painted as a pressed (which I suppose is better than just removing the ActionListener).
Upvotes: 2