0

I have a main class that calls a GUI so that users can enter in data. After the user is done and closes the GUI I need to preform other tasks but only after the GUI is closed. I keep seeing people have some of the same problem and be recommended to use a WindowListener and wait for windowClosing but that seems to be just for inside the GUI itself and I can't find any examples for how the my main class would be able to react to it. I tried approaching it as an actionListener but I wasn't able to find anything that would tell the calling thread. Am I missing something and if so what?

Here is the gist of what I have so far:

EditData (The GUI):

public class EditData extends JFrame implements ActionListener, 
                                                WindowListener
{
    public void windowClosing(WindowEvent e)
    {

    }
}

MAIN:

public class main
{
    public static void main(String[] args)
    {
        //Launches my GUI
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                EditData newEvent = new EditData(ID, conn);
            }
        });

        //After the user closes newEvent, I need to do something

    }
WickedChester
  • 35
  • 1
  • 5
  • 1
    *"I have a main class that calls a GUI so that users can enter in data."* Use a modal `JDialog` instead. (I'd suspected that was the best approach even from reading the title.) A modal dialog will cause the code to 'stop' at the exact moment that dialog is displayed, the very next line after it is shown will be called the moment it is closed. That's when to collect (check etc.) the data, and proceed. – Andrew Thompson Nov 14 '17 at 23:28
  • *"Any ideas?"* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). I note that @MadProgrammer has closed this as a duplicate, so if you can get a non-functional example together that we can see failing / work with, it might have to be posted on a new question thread. – Andrew Thompson Nov 15 '17 at 00:24
  • @AndrewThompson That seems to fix one problem but caused 2 others. I now extend JDialog instead of JFrame and I set this.setModal(true) but now for some reason none of my objects display and when I close the dialog, the thread still runs when I set this.setModal(true) where I setup the Frame but when I put that line at the end of my constructor, it works as intended. Do you know why it works like that? EDIT: Clarified the question – WickedChester Nov 15 '17 at 00:25
  • 1
    @WickedChester At this point, it would be better to create [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and post a new question with it so we can better understand how you are approaching the problem – MadProgrammer Nov 15 '17 at 00:27
  • *"Do you know why it works like that?"* I'll put as much as an iota's further thought into that question, once I have an (i.e. **your**) MCVE / SSCCE compiled in my IDE. – Andrew Thompson Nov 15 '17 at 00:27
  • @AndrewThompson Thanks for your help; I think I got this figured out. For some stupid reason I had to move my this.setVisible() to the end of everything instead of where I setup my frame. – WickedChester Nov 15 '17 at 00:44
  • Ah right. There are a lot of good reasons as to why calling `setVisible(true)` should be the last thing done for a top level container like `JFrame` or `JDialog`. Glad you got it sorted. :) – Andrew Thompson Nov 15 '17 at 01:15

0 Answers0