Solved

How to communicate between 2 JFrame in Java Swing?

Posted on 2007-04-06
6
1,797 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 60
servlet example 17 59
Eclipse Java import and method not resolved 4 82
Eclipse for Java EE development 2 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

730 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