Invoke a method of a JFrame from a JInternalFrame without do a new Instance

924 views Asked by At

I have a little big problem doing this:

From a JFrame, I call a JInternalFrame, so, I want to call a method of the JF from the JIF. The problem is, if I make a new instance of this JF, the changes are not applied, because it's not the same original instance.

I'm doing a login in this JIF, if the credentials are right, the menu in the JF must change, otherwise, no change must be applied.

I read in other sites on Internet that "Invoke" is the solution, but "Invoke" apparently works only with new instances. Is there another solution?

2

There are 2 answers

0
Vlad On BEST ANSWER

Thanks everybody for his help!

I've solved the problem!

Generally to call all my JInternalFrames I used a general global declaration:

private JInternalFrame jif;

But to solve my problem, I declared a new constructor using directly the class of my JInternalFrame:

private LogInJInternalFrame logIn;

Before the call, I declared a public global constructor in the Log In JIF:

public MainJFrame mainJF;

With that, using the new constructor in my JF, I could able to access to all the public declarations in my JIF. Then, I made the call using something like this:

private void logInCall(){
    logIn = new LogInJInternalFrame(); //--New instance of my JIF
    logIn.mainJF = this;  /*Set the class to the constructor instead to declare a new one*/
    logIn.setVisible(true);
}

After that, in my JIF, when the credentials were right, I've used this:

mainJF.setNewUserMenu();  /*The class's instance let me "invoke" the method I've wanted*/

And that's all, after the results all I can said was "I really need to relax more and think better the things".

Thanks again!!!

1
Hovercraft Full Of Eels On

Don't think in terms of JFrame or JInternalFrame because if/when you expand your program, you will likely not be using any classes that extend either of these two classes. Instead you should look on this in the general light of how does one object call a method of another object. The solution is for one object to get a valid reference to the object of interest, perhaps by passing that reference in through a constructor parameter or setter method. Then call your desired public method on that reference. In this JFrames and Swing is no different from any other Java program.

Regarding,

I read in other sites on Internet that "Invoke" is the solution...

No, that has nothing to do with your current problem but rather with Swing threading issues, which again is not the reason your current code is failing.

For more help and better help, consider creating and posting an sscce.