Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDialog: dispose() in finalize() and jvm exiting problem

843804Dec 1 2004 — edited Dec 9 2004
Hello!

I've created dialog which shows TIF image using JIMI library.
I'm invoking dispose() in finalize() method of this dialog. It couse problem with exiting application - System.exit(0) is invoking, but jvm is still working!!! If there is no dispose() in finalize() there is no problem. I check that if I don't use Jimi library everything works fine. It looks like problem with using dll-s (not exactly Jimi library).
I need to invoke dispose() in finalize() (not exactly in this case of course) because I want to be sure that dialog is cleaned up before exit.
Here is code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.*;

import com.sun.jimi.core.Jimi;

/**
 * @author <a href="mailto:kubak@kompakt.pl> Kuba Kr�likowski</a>
 */
public class ImageTest extends JFrame {

  public ImageTest() {
    getContentPane().add(new JLabel("Test"));
    pack();
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.out.println("finishing");
        System.exit(0);
      }

    });
    setVisible(true);
    createDialog();

  }

  protected void createDialog() {
    JDialog jd = new JDialog(this, false) {
      protected void finalize() throws Throwable {
        super.finalize();
        dispose();
      }
    };
   // it doesn't change anything
    jd.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    jd.getContentPane().setLayout(new BorderLayout());
    try {
      Image img = Jimi.getImage(new FileInputStream("c:\\Temp\\tresc2.tif"));
      jd.getContentPane().add(new JScrollPane(new ImagePanel(img)), BorderLayout.CENTER);
    } catch (Exception x) {
      x.printStackTrace();
    }

    jd.pack();
    jd.setSize(new Dimension(400, 400));
    jd.setLocationRelativeTo(null);
    jd.setVisible(true);
  }

  public static void main(String[] args) {
    new ImageTest();
  }

  class ImagePanel extends JPanel {

    protected Image img;
    protected int imagePanelWidth, imagePanelHeight;

    ImagePanel(Image img) {
      super();
      this.img = img;
      if (this.img != null) {
        this.imagePanelHeight = img.getHeight(this);
        this.imagePanelWidth = img.getWidth(this);

        setSize(imagePanelWidth, imagePanelHeight);
        setPreferredSize(new Dimension(imagePanelWidth, imagePanelHeight));
      }
    }

    public void paint(Graphics g) {
      super.paint(g);
      Graphics2D g2 = (Graphics2D) g;
      if (img != null)
        g2.drawImage(img, 0, 0, this);
    }

  }
}
"test2.tif" is small TIFF image.
Run class and try to close "test" window - System.exit(0) will invoke but jvm will still run.

Do you have any idea? Is it a java bug?

Kuba Kr�likowski
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2005
Added on Dec 1 2004
11 comments
541 views