Andy
Andy

Reputation: 3682

Creating a GUI with ActionListeners inside a method

I have a method inside one of my classes to my Java application that makes a Swing GUI and has it's own action listeners - which works fine. However, when the window is closed I need the method to return a String[] array; this is the part that is causing the problems...

I have added a simple return statement at the end of the method, but obviously Java doesn't wait for the action listeners and thinks that the method is complete once the action listeners have been added. So is there any way to "hold" a method, and then resume it when I am ready - or even, a different solution to my problem?

Thanks in Advance

Upvotes: 4

Views: 680

Answers (3)

Neifen
Neifen

Reputation: 2595

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

as 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);
        }
    }
}

Upvotes: 3

Andrew Thompson
Andrew Thompson

Reputation: 168825

Use a modal JDialog or a JOptionPane instead. The code that opened it will pause at that point - until the modal component is dismissed from the screen.

Upvotes: 4

mre
mre

Reputation: 44240

You can add a WindowListener to the JFrame instance and override the windowClosing(WindowEvent e) method. And therein, you can implement your own behavior.

Upvotes: 2

Related Questions