Link to home
Start Free TrialLog in
Avatar of khoama
khoama

asked on

How to communicate between 2 JFrame in Java Swing?

Anybody know how to communicate between 2 JFrames using Java Swing?I have tried many way but not successful.For instance, there are 2 forms called MainForm and subForm.When I do an action in MainFrom, subForm appears and allow me to input data or chosing a file.Then I want the MainForm know which file chosen or data entered when subForm closed.

Just because when subForm returns data to MainForm,a new instance of MainForm initialised and vice-versa   ( MainForm frm = new MainForm ).So the returning data from subForm to a second MainForm which is not needed.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> When I do an action in MainFrom, subForm appears and allow me to input data or chosing a file.
>> Then I want the MainForm know which file chosen or data entered when subForm closed.

Can you not just open a JFileChooser? Does the second frame need additional fields compared to a JFileChooser? If so, you can make it a modal JDialog
Avatar of khoama
khoama

ASKER

First,thanks for your reply.

Your answer could works. But how can the first JFrame know when the second is closed or not visible.The idea here is when the second JFrame chose an image for the first JFrame to display,and when I press Accept button.The second JFrame will be dissapear(setVisible(false)) and returns the Image file path to the first.

Thanks.
Avatar of khoama

ASKER

Yeah I can  just implement a JFileChooser.But I want some features in the second frame.That's why I didn't chose a JFileChooser.If there is no other way, I will use a file chooser.
Use a modal JDialog - it will take care of all you want
Avatar of khoama

ASKER

Thanks for your comments,I'll tell you the whole picture again.
I have 2 form called MainForm and SubForm.
To simplify i assum that MainForm just has 2 components : a button called AddNewPicture and a JScrollPane to display image
The SubForm has 2 buttons called Browse and Add and some textfields (the number of textfield is not important).
And here is what i expect.
1.When i click button AddNewPicture in MainForm, the SubForm opened.
2.Then i click  Browse button in SubForm a file chooser dialog appears  to choose the image which i want to display.
3.Next i input some information in textfields of SubForm.
4.Finally when i press button Add in SubForm all information of the picture will be store in database and the JScrollPane in MainForm will display the image which i choose.

The problem is that i can not pass the path of the image from SubForm to MainForm. How do you think about getParent method? Maybe it can solve this problem but i dont know how to use it. Thanks for comments