954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Modifying a Button

I'll make it short.
First is how do I change the text colour of a disabled button. I've tried HTML and setForeground but doesn't work.
Second, when I want to click a button, I can't move my cursor or the event will not fire. I used addMouseListener because I also need to handle a right click.
Third how do I add an icon to the button when I click it.

Thanks.

Agent Cosmic
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

I have never tried what you want to do but a quick look at the API will get you what you want.
Have you tried looking at the API of the JButton class. If you look at the constructor there is one that accepts an Icon. If you search the page for a setIcon method you will find the class: AbstractButton. JButton extends that class and has access to all of its methods.
If you look at its API you will find several setIcon methods that your JButton instance can call.
Also you will find a setUI method.

Do a little digging and tell us what you have found.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Here's the solution I found. Instead of using setText, I used a label so the colour wouldn't change.
Second, instead of using the mouseClicked event, I used the mouseReleased event so it would still fire.
Well third is using setIcon.

But here's the problem. Is it possible for the icon not to change colour when the button is disabled, it becomes light gray.

Agent Cosmic
Junior Poster in Training
51 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: