简体   繁体   English

调用禁用的JButton的actionlistener

[英]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. 我正在做一些需要从另一个函数调用禁用的jbuttonactionlistner的事情。 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. 制作一个新的方法,该方法将由禁用的jbutton调用,并在其中单击该按钮时将执行的所有代码都写入其中。 You can not call the actionlistiner in other way. 您不能以其他方式调用actionlistiner

...
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 您不能disabled GUI控件上调用/执行actions ,这实际上是disable意思

What you can do is create a separate common method like doClick() and call where ever you need. 您可以做的是创建一个单独的通用方法,如doClick()并在需要的地方调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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