-3

I placed an actionListener on some JButtons I created. It is such that if the user clicks a button, another class is called-up. I want to detect if that class has finished its function... for more clarity, here is my code:

Quest.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent iq) {

        JButton source = (JButton) iq.getSource();

             if (point.equals(points.get(0))){

         q1 = new Quest1();  //class called up
         source.setEnabled(false);

         //This is where my problem lies.... I want to be able to detect when the button's action is finished...than some other action takes place!
}
});

please help me.....

thePatriarch
  • 97
  • 1
  • 12
  • That is it!... people downvote without reasoning! – thePatriarch Oct 04 '16 at 16:50
  • 2
    People downvote because your question is actually not very precise; almost very close to "unclear what you are asking". Please understand: you should see *any* downvote or close request as **feedback** to you about the quality of your input. – GhostCat Oct 04 '16 at 16:54
  • Are you wanting this action to detect whether there are other actions registered on the button, and wait until all of them have completed? – erickson Oct 04 '16 at 17:08
  • "I've applied your code... still not working as desired!" Then post a [mcve] with expected results, instead of expecting mind-reading. – erickson Oct 04 '16 at 17:10

3 Answers3

3

You already wrote the code to that!

You see, unless you put in extra effort, a method that calls another method (like in this case: using new to construct another object) does things in sequence!

Meaning: your last line with the comment in your listener is reached after all other things in your method happened!

The only thing to be aware of: if the constructor of Quest would be creating and starting another Thread, then of course, things would be different. Then you would need hand-crafted communication between the threads involved. But I am somehow guessing that this is not the case here.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • This is the correct answer... Unless you're spinning up another thread somewhere, the method will be done on the next line. – Christopher Schneider Oct 04 '16 at 17:01
  • Exactly!!.... I'm calling up another class somewhere... and I want to detect if that class has finished executing, then something else takes place! – thePatriarch Oct 04 '16 at 17:02
  • What do you mean exactly? Are you starting another thread or aren't you? Instantiating another class doesn't inherently run on another thread. As far as the code you've provided, everything runs top to bottom. – Christopher Schneider Oct 04 '16 at 17:02
  • @Presh_K7 See, and that part is totally unclear in your question. **You** own that code; so you should understand what that constructor is doing! And you are really wondering why people downvote you for such input? – GhostCat Oct 04 '16 at 17:07
1

Change a boolean completed to true then check for that.

joe pelletier
  • 390
  • 2
  • 12
  • Sorry: if he is not doing multithreading, that is totally useless; and when he is doing multi-threading, things might be much more difficult. So in any case; your answer doesnt help much. As a rule of thumb: answers that make up only a single sentence **never** make up a good answer. – GhostCat Oct 04 '16 at 17:06
  • Why doesn't he just put the code he needs to run in the event. I assume he wants to check if it has ever completed or is not currently running – joe pelletier Oct 04 '16 at 17:13
0

Why don't you just call a method there?

Quest.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent iq) {
    JButton source = (JButton) iq.getSource();
    if (point.equals(points.get(0))){
      q1 = new Quest1();  //class called up
      source.setEnabled(false);
    }
    onButtonActionDone()
});

private void onButtonActionDone() {
  ...
}
Mike Laren
  • 8,028
  • 17
  • 51
  • 70