简体   繁体   中英

Communicating between open classes

I have three classes. One is a worker class that does all the hard work but doesn't display anything. The other two are GUI classes one of which calls the other. The one that calls the second GUI class has the worker class open.

The first GUI calls the second with this method:

protected void openAdd() {

        AddPet add = new AddPet(ADD_PROMPT, FIELDS, DATE_PROMPT);
        add.setVisible(true);
    }

The second GUI class is used to get information from the user that is used in the worker class but, as I already have the worker class open in the first GUI I don't want to open it again and I want to use some of the information in the first GUI.

What I need to do is pass that information in the second GUI back to the first GUI so that it can work with it and pass it to the open worker class.

How do I do this?

EDIT: I think the best option would be to call a method in the first GUI from the second GUI but I don't know if this is even possible.

On second thought, it looks like your second window is being used essentially as a dialog off of the first window, and that you're using it for user data entry and little else. If so, then make sure that the second window is not a JFrame but is rather a modal JDialog. If so, then it will block user interaction with the first window when it's open, and extracting information out of it will be easy, since you're know exactly when the user is done with it, since program flow will resume in the first GUI immediately following your code that sets the second window visibile.

eg,

// in this example, AddPet is a modal JDialog, not a JFrame
protected void openAdd() {
    // I'm passing *this* into the constructor, so it can be used ...
    //    ... in the JDialog super constructor
    AddPet add = new AddPet(this, ADD_PROMPT, FIELDS, DATE_PROMPT);
    add.setVisible(true);

    // code starts here immediately after the AddPet modal dialog is no longer visible
    // so you can extract information from the class easy:

    String petName = add.getPetName(); // I know you don't use these exact methods
    String petBreed = add.getPetBreed(); // but they're just a "for instance" type of code
    // ... etc

}

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM