Solved

How to communicate between 2 JFrame in Java Swing?

Posted on 2007-04-06
6
1,725 Views
Last Modified: 2013-12-29
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.
0
Comment
Question by:khoama
  • 3
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 18865780
If they are in the same application context, use getter methods if you want to retrieve some data from the other frame:

public class FirstJFrame extends JFrame
{
  private JTextField personName ;
  ..
  public void getPersonName ()
  {
    return personName.getText () ;
  }
}

In the other JFrame, you can use: String s = firstJFrameObj.getPersonName () ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18866568
>> 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
0
 
LVL 2

Author Comment

by:khoama
ID: 18866598
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:khoama
ID: 18866735
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18868856
Use a modal JDialog - it will take care of all you want
0
 
LVL 2

Author Comment

by:khoama
ID: 18869493
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now