I was testing the solution for the problem
Will new instance of JVM or reflection help in this case
What I encountered was that this solution does not work if I try to invoke an application such as Real Estate game . So in place of just adding the following code after invoking main of Applic2
Frame[] f2 = JFrame.getFrames();
for(Frame fx: f2){
if (fx instanceof JFrame) {
JFrame aFrame = (JFrame)fx;
aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
I created a asynchronous thread that keeps on changing the action JFrame.EXIT_ON_CLOSE to JFrame.DISPOSE_ON_CLOSE as shown below
import java.awt.Frame;
import javax.swing.JFrame;
public class FrameMonitor extends Thread{
@Override
public void run(){
while(true){
Frame[] f2 = JFrame.getFrames();
for(Frame fx : f2){
if(fx instanceof JFrame){
JFrame aframe =(JFrame)fx;
aframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}
}
}
and invoking this this thread instance by its start method in MyApp class. but the solution is not working. Still I am facing the same problem of all frame closing on closing one frame. Why is it happening any suggestions and how to overcome this??
Please go through the following problem :
Let me present the problem in more detail
Add the Real Estate game code to workspace
Add the following package to the RealEstate code
package MyApplication;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import edu.ncsu.realestate.gui.Main;
public class MYApp {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String arg[]){
FrameMonitor monitor = new FrameMonitor();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,200);
f.setVisible(true);
Class cls = Main.class;
Object[] actuals = { new String[] {} };
// cls.
Method[] mts=cls.getMethods();
for(Method m : mts){
//System.out.println(m);
}
Method m = null;
try {
m=cls.getMethod("main", new Class[] { String[].class } );
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.invoke(null,actuals);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Frame[] f2 = JFrame.getFrames();
for(Frame fx: f2){
System.out.println(fx.getTitle());
// fx.setVisible(false);
if (fx instanceof JFrame) {
// System.out.println("M here");
// System.out.println(fx.getTitle());
System.out.println(fx.getName());
JFrame aFrame = (JFrame)fx;
aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}
}
package MyApplication;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class FrameMonitor{
public FrameMonitor(){
Timer timer = new Timer(10000, new FrameMonitor2());
timer.setCoalesce(true);
timer.setRepeats(true);
timer.start();
}
public static class FrameMonitor2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
Frame[] frames = Frame.getFrames();
for (Frame frame : frames) {
if (frame instanceof JFrame) {
JFrame change = (JFrame) frame;
System.out.println("Before = " + change.getTitle() + " = " + change.getDefaultCloseOperation());
((JFrame)frame).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
System.out.println("After = " + change.getTitle() + " = " + change.getDefaultCloseOperation());
}
}
}
}
}
Now invoke the main of MyApplication package and which inturn invokes RealEstate game. This is where the solution does not work, try closing different RealEstate frames and see the entire application closes.