Sorin Grecu
Sorin Grecu

Reputation: 1034

Trigger method from another class

So,i have 2 classes with one jframe each.

In the main one,the user puts data inside some textfields and then saves the data in a file. In the second one he will be able to read the saved files and preview the data that is inside of them.I this second jframe i want a button that can send the file name to the main class and start a method that will read the data from the file and put it back into the textfields so it can be editted.

So,i need to send some data(a string,filename) to another class and trigger a method inside that class/jframe based on that data.How can i do that ?

Thanks and have a good day !

EDIT :

Doing this :

Second class :

 Test2 test2 = new Test2();
 JOptionPane.showMessageDialog(this,"-"+ serializedPath+"-");//to see if the path is right
 test2.editare(serializedPath);

It is not triggering the method in my first jframe. I tried triggering the method from the same class with the same path,it works just fine. What am i doing wrong ? Isn't it trying to trigger it inside the second class ? It gives no errors though...

Upvotes: 0

Views: 1166

Answers (1)

Andrey Chaschev
Andrey Chaschev

Reputation: 16516

JFrame is a regular Java object. When you create frames, you can pass it to be called later:

JMainFrame jMainFrame = new JMainFrame(...);
JSecondaryFrame jSecondaryFrame = new JSecondaryFrame(...);
jSecondaryFrame.setMainFrame(jMainFrame);    // sets the object field value

Then in an ActionListener you could access this jSecondaryFrame.jMainFrame.readData().

Upvotes: 1

Related Questions