Creating a GUI using ActionListeners inside a method

I have a method inside one of my classes for my Java application that creates a Swing GUI and has its own action listeners - this works great. However, when the window is closed, I need a method to return the String[] array; this is the part that causes the problems ...

I added a simple return at the end of the method, but, obviously, Java did not wait for action listeners and thinks that this method will be completed after adding action listeners. So, is there a way to β€œhold on” the method and then resume it when I'm ready, or even another solution to my problem?

Thanks at Advance

+4
source share
3 answers

Use a modal JDialog or JOptionPane instead. The code that opened it will be suspended at this moment - until the modal component is removed from the screen.

+4
source

try with WindowListener , so when you close the window, you can send your array

as an example:

 public class YourClass{ ... window.addWindowListener(new NameOfListener()); ... class NameOfListener() extends WindowAdapter{ @Override public void windowClosed(final WindowEvent e) { // send your array anInstanceYouWish.setArrayXY(yourStringArray); } } } 
+3
source

You can add a WindowListener to the WindowListener instance and override windowClosing(WindowEvent e) . And in this case, you can implement your behavior.

+2
source

All Articles