JFrame not responding to dispose() function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

JFrame not responding to dispose() function

amitSinghamitSingh IndiaPosts: 1Member

I am trying to code a program which pops up a file chooser window or dialog box to select a file first, if the user clicks 'cancel' or closes the window then the parent JFrame would not show up. Code:

import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.UnsupportedLookAndFeelException; public final class base extends javax.swing.JFrame { @SuppressWarnings("LeakingThisInConstructor") public base() { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { try { javax.swing.UIManager.setLookAndFeel(info.getClassName()); } catch (ClassNotFoundException ex) { Logger.getLogger(base.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(base.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(base.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(base.class.getName()).log(Level.SEVERE, null, ex); } break; } } initComponents(); setResizable(false); chooseFile(this); } @SuppressWarnings("unchecked")
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Java Image Viewer 1.0");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}                        
public void chooseFile(base Base)
{
    int ret=new JFileChooser().showOpenDialog(this);
    if(ret==JFileChooser.CANCEL_OPTION)
        Base.dispose();
}
public void abort(base Base)
{
    Base.dispose();
}                  

}

The problem is that the JFrame shows up even if I click cancel in the file chooser. I am using Netbeans. Apologies for my poor English.

Sign In or Register to comment.