Click to See Complete Forum and Search --> : Different Actions on the same Jbutton
daina
11-18-2005, 08:50 AM
Hai,
I have a Jbutton Say "Click Me".When I click on that button, some images will come.When again I click on that button (Click me) , all the images which added should go.Can anybody tell me how to do it.Only one button can used. We can use button or something else.
Regards
Daina
You could either just have one action that does different things depending on the state of the display or you could have two sepearate actions and change the action that is attached to the button after each event is run.
daina
11-18-2005, 09:13 AM
Hai
Thanks for your reply.But I didn't get what u mean.Can u give some sample code....
I cant give you code because I have no idea how your program is structured. If you want a more specific answer you will have to attach code samples and more information about the general structure of your program.
I can give you the method to set the action for a JButton but I assume you already have that information.
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/AbstractButton.html#setAction(javax.swing.Action)
Here is some sample action code.
http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/index.html#ActionDemo
I can't read minds.
Cytael
11-23-2005, 07:11 PM
Easiest solution that comes to mind would be to keep a boolean flag that toggles every time the button gets clicked. something to the effect of:
boolean imgFlag = false;
public actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == yourJButton)
{
if(imgFlag == false) { showImages(); imgFlag = true; }
else { hideImages(); imgFlag = false; }
}
}
Add sugar to taste and to fit your program. :)
daina
11-24-2005, 01:54 AM
Hai cyael,
Thanks for the help .its working now.