• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating button with dual purpose

 
son dunckley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have created a button 'Start Search' and when you click on it, a Binary Search Tree is displayed.

However, I would also like this button to then change to 'New Search' - I have done this by just changing its label text when the button is clicked.

Now I want to set it up so that if 'New Search' button is clicked it generates a new tree but I don't know how to tell the actionPerformed method that this button is being clicked a second time(or as the New search button) so now do a different set of instructions.

Am I approaching this wrong? Should I actually create a brand new button once the 'new search' button is clicked and place it in the same area, then I can call the 'new search' button.

Hope someone can help me with where to go with this.

Thanks
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I do not see the difference in the described actions to be performed the first and the second time.

Anyway - resist the temptation to write a boolean hasRun and to use an if-else. Instead use a state pattern: Make two different Actions (different in state or runtime class) and set the second action after the first actionPerformed (e. g. as the last instruction in actionPerformed).

Kai
 
Henry Addo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set a counter so that when the Star Search is clicked the counter
is increased by say one then when the New Search is clicked the counter
is reset. Hope that it solves
 
son dunckley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the replies. I will look into those and see if I can get it working - will be back if I can't !

The difference for the buttons is a tree is generated as soon as the applet is started, when they 'start search' button is selected - the tree is displayed. Once it is on display the button changes to 'New Search' - I then need to be able to call the methods for generating a new tree and then displaying it so the second call to the button does a little bit more. Hope that my question makes a bit more sense now - sorry i didn't explain it properly the first time round.

Cheers
[ August 06, 2005: Message edited by: son dunckley ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have done this by just changing its label text when the button is clicked.

identify the 'to-do' action from the label
something like this

if(button.getText.equals("StartSearch"))
{
do something();
}
else doSomethingElse();
 
son dunckley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael

That looks like an excellent idea - exactly what I was hoping to be able to do but didn't know how.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!