tags:

views:

443

answers:

2

I have a jbutton which performs a function when clicked on by mouse. For doing this programatically I have this other function

void clickButton(){
      backButton.doClick();
}

When I run the clickButton() function I can see the backButton being pressed on the jFrame but the function associated with backButton does not happen. When I click on it with mouse it functions. what am I doing wrong here?

A: 

How are you attaching the logic to the button? If you are using an ActionListener (or Action) it should fire. If you are using something else (perhaps MouseListener?), I don't think it will.

Dan Dyer
A: 

If you have a ActionListener attach to your button it'll be fire when you call the method .doClick();

A sample test to prove it:

public class Test implements ActionListener{

public Test(){

}

public void actionPerformed(ActionEvent e) {

 System.out.println("The action have been performed");

}

public static void main(String[] agrs){
 JButton but = new JButton();
 but.addActionListener(new Test());
 but.doClick();
}

}

Nettogrof