Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disabled button still clickable

 
Cecilia Anderson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic