my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Disabled button still clickable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Disabled button still clickable" Watch "Disabled button still clickable" New topic
Author

Disabled button still clickable

Cecilia Anderson
Greenhorn

Joined: Nov 11, 2001
Posts: 13
Hi!
I have a very annoying problem. I am using an ordinary JButton with a MouseListener added.
The button should only be clickable under certain conditions so I use the setEnabled(true/false).
Result: The apperance of the button changes but I am still able to click it hence getting an exception from my listener.
What is wrong???
//Cecilia
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
hi cecila,
it should work!!
lest say u have first disabled the button by setting
button1.setEnabled(false);
and in some event say ActionEvent for another button, upon clicking this 2nd button u want to enable the first button, u do
button1.setEnabled(true);
eg.
button2.addActionListener(this);

public void actionPerformed(ActionEvent ae){
if(ae.getSource()==button2)
button1.setEnabled(true);
}

//
if u show me a snippet of the code, i can help u
rgds,
Shashi
Cecilia Anderson
Greenhorn

Joined: Nov 11, 2001
Posts: 13
Here is my code...
JButton search = new JButton("Search");
search.addMouseListener(new SearchListener());
search.setEnabled(true);
Later on I use:
search.setEnabled(false);
The button appears to be disabled but as I said if you click on it, it is still active.
Seems to me that after I add the MouseListener it doesn�t matter if the button is disabled, it will still try to perform the event.
//Cecilia
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I think you want to be adding an ActionListener, not a MouseListener.

The ActionListener is a higher level semantic event, and as such does the right thing when your button is disabled, ie, it can't be triggered.
The MouseListener is a lower event. It's also working as designed. Just because you have disabled the button doesn't prevent people from trying to click on it, and the MouseListener is just obediently informing you that a click has occured. The fact that it's disabled is part of the state of the button, but doesn't affect the event the MouseListener is trapping.
[ May 11, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
hi cecila,
as pointed out by Rob, u should add ActionListener instead for a button, and not a MouseListenener!!
just try to change ur code according to what i posted b4, and see.
if u still have any problem, just come back to the to Saloon!!
rgds,
Shashi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disabled button still clickable