Click to See Complete Forum and Search --> : [RESOLVED] Running a JButton automatically


blaze12364
March 27th, 2008, 06:53 AM
Hi i want to be able to automatically activate a JButton and run the commands under that button when new button 'All Targets' is selected.

I have the following code, can anyone help?



JButton all_tar = new JButton ("All Targets");
all_tar.addActionListener(this);
JButton calcuate = new JButton ("Calculate");
calculate.addActionListener(this);

if (arg == "All Targets")
{
for (m = 100; m < 1000; m++)
{
tgetbox.setText(" " + (int)(m));
target = m;

//here is where i want to automatically activate another button

}
}

if (arg == "Calculate") //this is the button that i want to automatically activate numerous times

{ //instructions
}

keang
March 27th, 2008, 07:16 AM
There are a few ways of doing this but probably the simplest is to call the button you want to activate's doClick() method which programatically clicks the button.

blaze12364
March 27th, 2008, 08:33 AM
There are a few ways of doing this but probably the simplest is to call the button you want to activate's doClick() method which programatically clicks the button.

I don' t understand how you would implement doClick() with a button that i have created, i know you can use it easily when its a button being pressed on a keyboard using keyevent. Could you explain ?

thanks for the quick reply

dlorde
March 27th, 2008, 08:47 AM
I don' t understand how you would implement doClick() with a button that i have createdYou don't have to implement it, just call it - it's part of the AbstractButton interface: doClick() (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractButton.html#doClick()).

That is the essence of science: ask an impertinent question, and you are on the way to a pertinent answer...
J. Bronowski