How to prevent JFrame from closing

I have a Java GUI application from which another GUI GUI application is called using reflection and loading. It works fine, the only problem is that closing the JFrame called application also closes the Main GUI application frame. How can I prevent the main application (frame) from closing?

I cannot change the defaultCloseOperation called application, but a change to the main application can be made. Does it have anything to do with threads?

enter image description here

This is my application code that runs the target application

 public class ClassExecutor{ private ClassLoaderOfExtClass classLoader; private byte[][] ArrayOfClasses; private String[] ArrayOfBinaryNames; @SuppressWarnings("rawtypes") private ArrayList<Class> loadedClasses; private ArrayList<String> loadedClasesNames; private Object[] parameters; @SuppressWarnings("rawtypes") public ClassExecutor() { classLoader = new ClassLoaderOfExtClass(); new ArrayList<Class>(); loadedClasses = new ArrayList<Class>(); loadedClasesNames = new ArrayList<String>(); } @SuppressWarnings("unchecked") public void execute(File[] file, String[] binaryPaths) { Object[] actuals = { new String[] { "" } }; Method m = null; try { Field classesx=ClassLoaderOfExtClass.class.getDeclaredField("classes"); classesx.setAccessible(true); } catch (SecurityException e1) { e1.printStackTrace(); } catch (NoSuchFieldException e1) { e1.printStackTrace(); } /*for (int i = 0; i < file.length; i++) { for (int j = 0; j < file.length; j++) { try { @SuppressWarnings("rawtypes") Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]); //Fied classex=classLoader.getResource("classes"); }catch(Exception e){ } } } Class<?>[]classesxx= getLoadedClasses(classLoader); System.out.println("Loaded classes have size "+ classesxx.length);*/ for (int i = 0; i < file.length; i++) { try { @SuppressWarnings("rawtypes") Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]); try { if (c.getMethod("main", new Class[] { String[].class }) != null) { m = c.getMethod("main", new Class[] { String[].class }); } else { System.out.println("This class does not contain main"); continue; } } catch (NoSuchMethodException e) { // System.out.println("Main not found!!!"); // System.out.println("M here"); // e.printStackTrace(); // not printing stack trace } catch (SecurityException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { System.out.println("No such class definition exist!!"); // TODO Auto-generated catch block // e.printStackTrace(); } } try { m.invoke(null, actuals); // CallStack.print(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings({ "unchecked", "rawtypes" }) public void execute(ArrayList<byte[]> stuffedFiles, ArrayList<String> binaryPaths) { convertToArray(stuffedFiles, binaryPaths); loadAllClasses(ArrayOfClasses, ArrayOfBinaryNames); Object[] actuals = { new String[] { "" } }; Method m = null; /* * Method[] m1= new Method[10]; for (Class c : loadedClasses) { * m1=c.getMethods(); } for(Method m2: m1){ * System.out.println(m2.getName()); } */ /* System.out.println(loadedClasses.size()); */ for (Class c : loadedClasses) { /* * System.out.println(c.toString()); * System.out.println(c.getConstructors()); */ // for (int i = 1; i < file.size(); i++) { /* * for(Method meth : c.getMethods()){ meth.setAccessible(true); * * } */ try { if (c.getMethod("main", new Class[] { String[].class }) != null) { m = c.getMethod("main", new Class[] { String[].class }); break; } else { // System.out.println("This class does not contain main"); continue; } } catch (NoSuchMethodException e) { System.out.println("Program does not contain main"); } catch (SecurityException e) { e.printStackTrace(); } } try { if(parameters==null){ m.invoke(null, actuals); } else{ try { System.out.println("It Fails Here"); m.invoke(null, parameters); } catch (Exception e) { System.out.println("Illegal arguments"); } } // CallStack.print(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+7
source share
3 answers

I am not allowed to make changes to the called application.

It was a comment in response to @JeffLaJoie, just to clarify, it will not require any changes to the code of another application. Just an extra method call or two from your application. runtime to set the close operation of the third-party operation.


Otherwise, the best solution I can come up with is to start a new frame in a separate Process , which starts a new JVM when the user closes another application. he and the second JVM will end, leaving the original application. on the screen.

+3
source

You want to use the DISPOSE_ON_CLOSE operation, so it will be setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)

EXIT_ON_CLOSE will be an option that closes all the windows that I believe are what you are currently experiencing.

+11
source

You have the following options for defaultCloseOperation :

  • DO_NOTHING_ON_CLOSE - operation to close the window "does nothing",
  • HIDE_ON_CLOSE - operation to close a window of a window of a hidden window;
  • DISPOSE_ON_CLOSE - The default window close operation for a window handle.
  • EXIT_ON_CLOSE - Close the default application window close window. Attempting to install this on Windows that supports this, such as a JFrame, may raise a SecurityException based on the SecurityManager. It is recommended to use it only in the application.

The DISPOSE_ON_CLOSE option can be used to not close all windows, closing only the one you need.

If you do not have direct access to the JFrame object, as you have with the last code hosted, you can use Window.getWindows () to get the whole instance of Windows (since JFrame is a Window too, it will also be specified). And then set defaultCloseOperation for this.

You may need to use threads, because defaultCloseOperation needs to be set after calling the main method.

Theoretically, this works, so I think this is a good shot;)

+6
source

All Articles