简体   繁体   中英

Calling actionlistener of a disabled JButton

I was working on something in which I need to call the actionlistner of a disabled jbutton from another function. How it can be done?

Make a new method which will be called by the disabled jbutton, write all the code there which will be executed when you click the button. You can not call the actionlistiner in other way.

...
JButton disButton = new JButton("Disabled");
disButton.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    //do not write any statement here
    doSomething();
  }
});

...
private void doSomething() {
  //all action event execution code here
  System.out.println("I am in the action listener");
}

....

//in  the other method or another button click event call doSomething()
//even button is disables like
JButton Button = new JButton("Submit");
Button.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    doSomething();
  }
});

//or from another method
public void method() {
  doSomething();
}

You cannot call/do actions on disabled GUI controls.That is what actually disable means

What you can do is create a separate common method like doClick() and call where ever you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM