JButton enable/disable

Hi there!

I have disabled my JButton by calling the JButtonObject.setEnabled(false) but it's event handler gets fired even when I click on a disabled button,,,I don't wanna fire the event handler when the JButton is in the disabled state....can anybody please help?

Thanks in advance..

[306 byte] By [Kami_Pakistana] at [2007-11-27 6:26:03]
# 1

If you need further help then you need to create a

[url=http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example

Program[/url] (SSCCE) that demonstrates the incorrect behaviour.

Don't forget to use the [url=http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url]

so the posted code retains its original formatting.

Aniruddha-Herea at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
It's a straight fwd question,,I don't think anybody here wud need any code for it.....
Kami_Pakistana at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
lets see....
Aniruddha-Herea at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi Aniruddha friend!

It could be done this way,,,

private void jBtn_Clicked(java.awt.event.MouseEvent evt) {

if (jBtn.isEnabled())

{

//Execute code

}

else

{

//Do nothing

}

}

..if the JButton is in the disabled state,,,the code in the event handler won't execute.....

Thanks friend...

Kami_Pakistana at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

> It's a straight fwd question,,I don't think anybody here wud need any code for it.....

Your correct. Handling a button click is straight forwared. And if you would read the JButton API and follow the link to the Swing tutorial then you would find the proper way to respond to a mouse click on a button.

If you code isn't working then you are doing something strange and we are not mind readers and we are not going to waste time guessing what you may or may not be doing.

So if you don't want to post your SSCCE, then I guess you don't really want any help.

The code you posted above is not the correct way to handle a button click by the way.

camickra at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 6
> It's a straight fwd question,,I don't think anybody here wud need any code for it.....by not posting the code you've ended up with a poor workaround.that code would not go well at a job interview.
Michael_Dunna at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 7
Hi friends!You may be right but I didn't have much time to prepare an SSCCE,,that's why I asked for a quick help,,,,anyways thanks for your guidance,,next time I would post the code for you guys,,,Thanks,,,,
Kami_Pakistana at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 8

> You may be right but I didn't have much time to prepare an SSCCE,,

You miss the point. You are asking for help so you take the time to provide all the necessary information we need to solve the problem.

We are not going to waste time reading an incomplete question and quess what you may or may not be doing. Our time can be better spent helping someone else who posted a complete question.

The forum is not about making it easy for you, its about making it easy for us to answer questions.

camickra at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 9
Ok camickr friend thanks....
Kami_Pakistana at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 10
If the event gets fired even when the button is disabled, there's something really weird. Apparently you're firing the same event somewhere else in your code. Maybe you've added a MouseListener on the button?
java_knighta at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 11
> Maybe you've added a MouseListener on the button? Thats what his example code in reply 4 shows.That is why we are telling him that he is using the wrong approach to handle the clicking of a button. The tutorial shows the correct approach.
camickra at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 12

> Thats what his example code in reply 4 shows.

My apologies, I did not see the MouseEvent part.

Kami, listening for mouse events in this case is wrong because by default you can operate a JButton, and more generically any GUI component, with no mouse at all e.g. with the Tab and Spacebar keys.

java_knighta at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 13

> listening for mouse events in this case is wrong because by default

> you can operate a JButton, and more generically any GUI component,

> with no mouse at all e.g. with the Tab and Spacebar keys

That is still not the point. Yes, you need to handle key boards events to activate the button, but you also need to handle the clicking of the mouse. However, that it not done by using a MouseListener, that is done by using a ActionListener.

Read the "How to Use Buttons" tutorial like I suggested to the OP originally.

camickra at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 14

> That is still not the point. Yes, you need to handle

> key boards events to activate the button, but you

> also need to handle the clicking of the mouse.

> However, that it not done by using a MouseListener,

> that is done by using a ActionListener.

Exactly, that was my point. :-) Probably I didn't make myself clear.

I was just pointing out that there are many ways to operate a button, but the right way is through an ActionListener, which automatically manages the state of the button i.e. it won't fire the event if the button is disabled.

The OP apparently tried to reinvent the wheel by adding a MouseListener to the button and then firing the event if button is enabled. This is not the correct way to do that. Even if he added keyboard support this would still be cumbersome and uselessly complicated -- even if the code actually worked.

java_knighta at 2007-7-12 17:46:28 > top of Java-index,Desktop,Core GUI APIs...
# 15

hello my friend.

try to check whether your main method implements Action Listener. this is a must so don't forget this okies?

ex:

public class AddressBook implements ActionListener

{

}

then, you now enable/disable your buttons:

ex:

cmdAdd.setEnabled(true);

cmdSave.setEnabled(false);

cmdEdit.setEnabled(false);

cmdDelete.setEnabled(false);

just make sure that you place these statements on the right methods. for example i want to enable the Add button at the start of the program and disable the rest of my buttons:

ex:

private void showForm()

//This is the part where you call the method/function

{

cmdAdd.setEnabled(true);

cmdSave.setEnabled(false);

cmdEdit.setEnabled(false);

cmdDelete.setEnabled(false);

}

but if you want to disable a button after you have already clicked another button try this:

The situation here is when you click the Save Button and after clicking it, you decided to enable the Edit and Delete Button.

if(src == cmdSave)

{

cmdEdit.setEnabled(true);

cmdDelete.setEnabled(true);

}

i hope this helps. God bless you =)

dadaa at 2007-7-21 21:54:04 > top of Java-index,Desktop,Core GUI APIs...