Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > questions about the implications of multiple JFrames using dispose() on closing

Thread Tools

questions about the implications of multiple JFrames using dispose() on closing

Posts: n/a

I've come up against some perplexing questions caused by the creation
of multiple instances of JFrame.

obj is referencing a JButton click. This then loads a FileDialog for
opening an image and displaying it in an extended JFrame called
'ResizeFrame'. JFrame then loads a panel which displays the Image. The
code as it stands allows multiple instances of ResizeFrame.

ResizeFrame basically does most of the work and calls methods to load
the Image - which has been opened in it - to be drawn in ResizePanel.

ResizeFrame is coded to 'dispose' on closing.

What I am trying to understand is what happens when a frame is opened,
then closed, and then a new frame is opened. Does the instance of
ResizeFrame (all named the same) that was initially opened, still
exist, or has it been over-written by the new version.

Am I making a mistake allowing several JFrames to be opened at once? I
know that when a frame is disposed that its claims on screen resourses
are freed up - but if it is still existing even after it has been
disposed it must still be using some memory resources?

Also, what are the implications of having several objects existing with
the same name?

Another thing I've noticed. My ResizeFrame has a method for loading a
panel and then is made visible within ResizeFrame. What I've noticed is
that if I load a ResizeFrame Image, then close it, then open up the
same image file - that the picture will appear much faster than if I
open up a different image. I'm not understanding how this is happening!

I could change my code so that it is only possible to have 1
ResizeFrame open at a time - however this would require a considerable
amount of simplification (i.e. beyond my hackish skills!). I would only
do it if I am convinced that the way I have done things is BAD with a
capital B (which it probably is - but then I am a very inexperienced

Thanks for any advice.


if (obj == newdepth) {
File f = loadimage("depth\\", FileDialog.LOAD);
if (!(f == null)) {
File q = new File(f.getParent());
if (q.isDirectory()) {
String w = f.getPath();
ResizeFrame jeff = new ResizeFrame((w + " - Depth Map"), new
Color(205, 219, 227), false);
else {
status.setText("invalid directory");

//--I've included the full code for ResizeFrame and ResizePanel hoping
for constructive critism. Also, it might help to make my questions

import java.awt.*;
import javax.swing.*;
import myroot.frames.*;
import java.awt.event.*;

import java.awt.image.*;

import osbaldeston.image.BMP;

public class ResizeFrame extends JFrame implements ImageObserver {
public Image myimage;
public ResizePanel mypanel;
public Color col;
public boolean resizing;

public void LoadPic(String inpic){
myimage = null;

int typepic = getTypePic(inpic);

if (typepic == 1) {
BMP dave = new BMP(new File(inpic));
myimage = dave.getImage();
if (typepic == 2) {
myimage = Toolkit.getDefaultToolkit().getImage ( inpic);



public int getTypePic(String in) {
if (in.length() > 4) {
String test = in.substring(in.length() - 4, in.length());
test = test.toLowerCase();

if (test.equals(".bmp")) return 1;
if (test.equals(".gif")|| test.equals(".png") ||
test.equals(".jpg") || test.equals("jpeg")) return 2;

return 0;
public void WaitImage(Image im) {
boolean wait = true;
wait = ImageUtilities.waitForImage(im, this);

public void addpanel() {
if (resizing == true)
mypanel = new ResizePanel(myimage, col);

JScrollPane scroller = new JScrollPane(mypanel,

scroller.getVerticalScrollBar().setUnitIncrement( 16 );

if (resizing == false) {
int w;
int h;

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
if ((myimage.getWidth(this) +40) > d.width)
w = d.width;
w = myimage.getWidth(this) + 40;
if ((myimage.getHeight(this) +60) > (d.height -30))
h = d.height -30;
h = myimage.getHeight(this) + 60;


public void setSize(){

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setSize(d.width, d.height);

public ResizeFrame(String title, Color c, boolean res) {

resizing = res;
col = c;
if (resizing == true) {

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {



import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

public class ResizePanel extends JPanel{
public Image myimage;
public Color col;

public ResizePanel(Image im, Color c) {
myimage = im;
col = c;

public Dimension getPreferredSize(){
return (new Dimension(myimage.getWidth(this),

public void paint(Graphics g) {
int h =0, v = 0;
h = (this.getWidth()-myimage.getWidth(this))/2;
v = (this.getHeight()-myimage.getHeight(this))/2;
if (h< 0)
h = 0;

if (v<0) v = 0;
g.drawImage(myimage, h,v, this);



Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help required in using getRuntime.exec() and JFrames Rohit Gupta Java 2 06-24-2005 04:59 PM
how can multiple JFrames all have the same name? (previous post rephrased) rfractal30 Java 1 04-14-2005 12:35 PM
how can multiple JFrames all have the same name? (previous post rephrased) rfractal30 Java 0 04-13-2005 01:24 AM
Implications of not closing db connection? vivienne wykes Java 13 08-27-2004 11:28 PM
many JFrames and JDialog modal only for one of them Lukasz Indyk Java 6 01-23-2004 01:56 AM