Remove Top-Level Container on Runtime

Remove Top-Level Container on Runtime

Invoking dispose() allows the host platform to reclaim memory consumed by the heavyweight peer, but it can't do so until after the WINDOW_CLOSING event is processed on the EventQueue. Even then, gc() is a suggestion.

Addendum: Another way to see the nightmare is via a profiler. Running the example below with jvisualvm, one can see that periodic collection never quite returns to baseline. I've exaggerated the vertical axis by starting with an artificially small heap. Additional examples are shown here. When memory is very limited, I've used two approaches:

  • Emergent: Loop from the command line, starting a new VM each time.

  • Urgent: Eliminate the heavyweight component entirely, running headless and composing in a BufferedImage using 2D graphics and lightweight components only.

Sample Image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;

/** @see https://stackoverflow.com/questions/6309407 */
public class DialogClose extends JDialog {

public DialogClose(int i) {
this.setTitle("Dialog " + String.valueOf(i));
this.setPreferredSize(new Dimension(320, 200));
}

private void display() {
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
passSomeTime();
this.setVisible(false);
this.dispatchEvent(new WindowEvent(
this, WindowEvent.WINDOW_CLOSING));
this.dispose();
passSomeTime();
}

private void passSomeTime() {
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace(System.err);
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
int count = 0;
while (true) {
new DialogClose(count++).display();
}
}
});
}
}

destroy Jframe object

Is there a way in your user1.txt file to notice if that user is an administrator or not?
Your question isn't very clear, but you should be able to do something like this:

JFrame frame = new JFrame();
frame.dispose();

The compiler will literally dispose of this frame and automatically clean up using the garbage collector.

How to close a previous Frame when a new Frame is opened

Can somebody please tell me how can i do so so that once a new frame
is about to open the old frame closes..

  • use CardLayout, then there no reason to playing with Top-Level Containers, put there required number of JPanels, then just to switch betweens JPanel views

  • as @trashgod notified, really not good idea, way

  • otherwise have to clean-up (remove all contents) uselless container, because Top-Level Containers never will be GC'ed

Is it safe to dispose a JDialog in a try block, and then continue executing code in a matching finally block?

No, as far as you access only non-graphical objects such as string from your example.

Are native screen resources automatically released when the JVM exits?

Everything owned by a process is released when the process exits, unless you are talking about pathological operating systems like Netware 3 & 4.

How to get rid of dead windows in Java?

I found the following code inside the project:

private void disposeWindow(final java.awt.Window window) {
SwingEnvironment.runOnEventDispatchingThread(new Runnable() {
@Override
public void run() {
window.dispose();
window.setVisible(false);
window.removeNotify();

String oldName = window.getName();
if (oldName == null) {
oldName = "unnamed";
}

if (!oldName.startsWith("dead-")) {
window.setName("dead-" + oldName);
}
}
});
}

So this is the reason the windows where named 'dead-'.

However they were properly disposed and still showed up in the Windows Array that is returned by the java.awt.Window.getWindows(). To get rid of them, I had to create a separate ThreadGroup and create a separate AppContext via SunToolkit.createNewAppContext();. Disposing that AppContext also disposed the Windows properly in my case. See also http://kingsfleet.blogspot.de/2009/10/how-to-have-more-than-one-instance-of.html.

How to detect if one swing window is above another one

Simply use Window.isFocusableWindow(), to check as to which JFrame lies on top of all others.

Though I will also insist on not using more than a single JFrame, for any application :-)



Related Topics



Leave a reply



Submit