?
Solved

How to communicate between 2 JFrame in Java Swing?

Posted on 2007-04-06
6
Medium Priority
?
1,841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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