user4188835
user4188835

Reputation:

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.

Upvotes: 1

Views: 48

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

e.g.,

// 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

}

....

Upvotes: 1

Related Questions