2

I'm working on my first big multi-file Java program. When the program starts, my main file calls another file that creates a GUI, populates it and accepts information. It then is supposed to return an ArrayList, which my main file is supposed to use.

The issue is that my GUI method is immediately returning with an empty ArrayList, instead of waiting until after a button has been pushed.

This is currently how my ActionListeners work

close.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    System.exit(0);
  }
});

start.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    temp = pullInfo(txtOrder, txtRounds);
  }
});

Where temp is the ArrayList that I want to return at the end.

This is what I have in my main file:

 JFrame launch = new LaunchWindow(); 
 ArrayList<Integer> temp = ((LaunchWindow) launch).createGUI();
 rounds = temp.get(0);
 temp.remove(0);
 order = temp;
 connect();

I don't want it to run the rounds = temp.get(0); until after that ArrayList has been populated. How can I make it wait until a button is pushed?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
AndyReifman
  • 1,459
  • 4
  • 18
  • 34
  • GUI commands are generally asynchronous. – Powerlord Jul 06 '15 at 16:57
  • @Powerlord Great. That's not very helpful though. Are you saying there isn't a way to do what I want easily? – AndyReifman Jul 06 '15 at 17:02
  • put the code into start button's action lsitener – Madhan Jul 06 '15 at 17:02
  • These `rounds = temp.get(0); temp.remove(0); order = temp; connect();` – Madhan Jul 06 '15 at 17:10
  • @Madhan The only problem is that those lines are in a different file. Meaning I would have to return more variables to my main file. Also, I would run in to the same timing issue, just on a different line of code. – AndyReifman Jul 06 '15 at 17:11
  • put the above code in a function[for ex dothis()].pass the main's object to LaunchWindow then call object.dothis() from the action listener – Madhan Jul 06 '15 at 17:14
  • This looks like a similar question. Does the top response answer your question? http://stackoverflow.com/questions/6935918/swing-gui-doesnt-wait-for-user-input –  Jul 06 '15 at 17:03

1 Answers1

0

I ended up mixing the answer @Madhan had with something of my own (I think)

I call the GUI file in my main file:

JFrame launch = new LaunchWindow(); 
ArrayList<Integer> temp = ((LaunchWindow) launch).createGUI();

and then changed my start.addActionListener method, to just call the next method from there.

start.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    temp = pullInfo(txtOrder, txtRounds);
    int rounds = temp.get(0);
    temp.remove(0);
    ArrayList<Integer> order = temp;
    connect(order, rounds);
  }
});
AndyReifman
  • 1,459
  • 4
  • 18
  • 34