This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Making instantiated frames independent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making instantiated frames independent" Watch "Making instantiated frames independent" New topic
Author

Making instantiated frames independent

karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Hi all, This is my first post. I have got this problem. Say there are two GUIs.

Class A extends JFrame implements ActionListener
{
A()
{
// all the code to create gui is here.
}

public static void main(String args[])
{
A gui=new A();
}

public void actionPerformed(ActionEvent evt)
{
B gui2=new gui(); // B here is another GUI using Jframe
}
}


So when the an event is performed in the first frane and another gui is invoked, there will be 2 windows in total. So when we close the newly created one(in this case gui2) the older one also atomatically gets closed.. Is there anyway to make both these windows independent?



A java addict
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Is there anyway to make both these windows independent?

Yes, but not without A.java starting a completely new Thread which would call B.java's main method. You can make it so that closing B.java doesn't end A.java by using dispose() instead of a System.exit for B.java. But once A.java die, B.java will also die.

What is your purpose in making them completely separate? What are you trying to achieve. Maybe we can give you a better alternative?


GenRocket - Experts at Building Test Data
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
How do i invoke the main function of other class. I understand that Main method is static and it should be invoke along with Class Name but what about the argument that has to be passed?

There is no purpose behind my question other than the urge to gain expertise
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

If you don't have any arguments, just pass it an empty array. Note this is untested code.



Something like that anyway.
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Thanks for the prompt replies.


Is that the only solution?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

karthick Soundararaj wrote:Thanks for the prompt replies.


Is that the only solution?


No, you could also do something like Runtime.exec("somefile") where somefile is a batch file or shell script that kicks off the other Java application (B). Keep in mind that your frames won't really be able to communicate with each other as they will both be in their own VM.
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Ok.. Thanks a lot, but i would be great if the future versions of java brings in a better solution for making them independent.. If you come across any other solution do post it here...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

karthick Soundararaj wrote:Ok.. Thanks a lot, but i would be great if the future versions of java brings in a better solution for making them independent.. If you come across any other solution do post it here...


Why would that be great? I don't use any applications that work this way. Applications should encapsulate themselves into a finite set of frames that all belong to the same VM, IMHO.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I think it can be all a lot simpler. What happens when you close your frame B? Does it exit? In other words, have you somewhere written
If so, then it is just logical that closing frame exits the entire application. If you change the EXIT_ON_CLOSE to DISPOSE_ON_CLOSE your application should only be closed completely if all non-daemon threads exit - and the EDT thread won't exit because there is still a frame active.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Making instantiated frames independent
 
Similar Threads
Completely delete a frame without deleting the application
No action detected
Output from a class shown in GUI?
Quick question about creating and instance and passing it to a constuctor in a GUI
Need help with my Java assignment from the start- to be submitted in a week.