This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes creating button with dual purpose Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "creating button with dual purpose" Watch "creating button with dual purpose" New topic
Author

creating button with dual purpose

son dunckley
Greenhorn

Joined: Jul 27, 2005
Posts: 17
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

Joined: Jul 17, 2004
Posts: 354
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


Kai Witte's business website Kai Witte's private homepage Mock exam / preparation kit reviews
Henry Addo
Greenhorn

Joined: Feb 26, 2005
Posts: 24
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

Joined: Jul 27, 2005
Posts: 17
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jul 27, 2005
Posts: 17
Hi Michael

That looks like an excellent idea - exactly what I was hoping to be able to do but didn't know how.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating button with dual purpose
 
Similar Threads
How do you return from an inner method
how to delete an object from heap ?
keeping a button in pressed state
JSP Doubt
populate JComboBox and MVC