Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Disable Button from being clicked without altering image icon  RSS feed

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have an image (of a briefcase) button, however when I use:



it darkens the image icon of the button as well as disables it from being clicked. Is there a way to only disable the button from being clicked WITHOUT darkening the image?


Thanks in advance
 
Marshal
Posts: 59389
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The change in colour from black text to grey text is the usual behaviour of disabled buttons in most platforms. You can probably try changing background colour, transparency, etc. Or somebody else will know exactly what to do.

Moving to our GUIs forum.
 
Rancher
Posts: 296
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In javafx you could consume the event, but I don't know if it will work in swing
 
Rancher
Posts: 2911
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Icon is darkened to give the user a visual clue that the button is disabled. The user would get frustrated if they click on the Icon and nothing happens.

Having said that it is easy to prevent the darkening of the Icon.

However, of the previous questions you have asked in the forum, not once have you responded to an answer/suggestion with something like "that works, thanks for the help".

Given that you don't appreciate all the help you have received in the past, I will skip the answer to this question.
 
Master Rancher
Posts: 2696
92
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm,

Rob is right, helping someone and getting no feedback whatsoever can be frustrating. But this site should be friendly to all, so lets give it another go.

If you look at the API of class AbstractButton (of which the JButton is extended), you will find a method that deals with setting an icon in case the Button gets disabled. Now, you can use the same icon that is also used as the enabled icon, but that has the problem that Rob described (the user has no indication of the button being disabled). So this is also a nice way: make a copy of the enabled icon, draw a big red X over it, and set it as the disabled icon.
 
Caiz Austin
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies everyone, and for bringing that up Rob which I was unaware of. I'm really sorry!, it wasn't my intention to seem ungrateful, I am very grateful for all comments! When looking through other sites like stack overflow it says not to post comments that only say "thanks etc" so I just assumed it was the same for here. But I will reply to all my previous posts now so other users who may encounter these problems will know which solution worked

Also thanks Plet, I've used the .setDisabledIcon() method, it still darkens the image a bit but not so much as the default
 
Piet Souris
Master Rancher
Posts: 2696
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Caiz,

great reply, and have a well deserved cow!

Caiz wrote:Also thanks Plet, I've used the .setDisabledIcon() method, it still darkens the image a bit but not so much as the default


Hmm, it is quite a while ago since I did anything like this, so maybe my memory is playing tricks. I'll have a look at it and will let you know. But maybe Rob had a better way in mind, so let's hope he will let us know.
 
Rob Camick
Rancher
Posts: 2911
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used code like the following:



To me the icon is painted the same.

The difference I notice is that the background has changed.

Maybe a different solution would be to replace the button with a JLabel when you want to disable the button.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!