Solved

How to communicate between 2 JFrame in Java Swing?

Posted on 2007-04-06
6
1,695 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:
mayankeagle earned 500 total points
Comment Utility
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:mayankeagle
Comment Utility
>> 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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:khoama
Comment Utility
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:mayankeagle
Comment Utility
Use a modal JDialog - it will take care of all you want
0
 
LVL 2

Author Comment

by:khoama
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

762 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

10 Experts available now in Live!

Get 1:1 Help Now