Results 1 to 16 of 16
  1. #1
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default JFrame not responding to dispose() function

    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.

    Java 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();
        }
    }
    The problem is that the JFrame shows up even if I click cancel in the file chooser. I am using Netbeans.
    Last edited by amitSingh; 06-13-2014 at 02:59 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: JFrame not responding to dispose() function

    Please wrap your code in [ code] [ /code] tags.
    I can't follow unformatted code.

    Are you sure that code compiles?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    there is no error in the code as I am using netbeans and the program runs

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JFrame not responding to dispose() function

    Java Code:
          Base.dispose();
    This is actually correct code since the class is classed base and the parameter Base. But it's Confusing.MAX_VALUE.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: JFrame not responding to dispose() function

    Where is the call to setVisible()?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    Actually I invoke this class - base - from the main function of another class using setVisible()

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: JFrame not responding to dispose() function

    Quote Originally Posted by SurfMan View Post
    Java Code:
          Base.dispose();
    This is actually correct code since the class is classed base and the parameter Base. But it's Confusing.MAX_VALUE.
    Ha!
    Even with the tags that's hard to see...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: JFrame not responding to dispose() function

    Reversing the case of the first letter in the names is confusing.

    Is setVisible() called AFTER dispose()?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    I don't think the initComponents() function has anything to do with the problem as it is generated by the IDE. Probably I shouldn't use the JFileChooser object that way, not sure though.
    Last edited by amitSingh; 06-13-2014 at 04:01 PM.

  10. #10
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    The Main class of the project is

    Java Code:
    public class Test1 {
        public static void main(String[] args) {
            new base().setVisible(true);
        }
    }
    as it is called first of all, I think setVisible() is called before dispose().

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JFrame not responding to dispose() function

    Quote Originally Posted by amitSingh View Post
    The Main class of the project is
    as it is called first of all, I think setVisible() is called before dispose().
    False assumption. It is the constructor which shows the FileChooser, not the setVisible(). The moment you see the FileChooser, the constructor is still being executed. When you dispose() the Base instance, there is nothing to dispose, since it's not visible. When the constructor is finished, you call setVisible(true), which makes it visible.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    I am sorry but I didn't get it.
    'chooseFile(this)' is the last line in the constructor, that's where the constructor is finished, besides setVisible() is called just once that too in the Main class, as I am using Netbeans, the 'Test1' class, i.e. the Main class of the project, is called first and that's only where the setVisible() function is used.
    That's what I think, which might be wrong. ;)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: JFrame not responding to dispose() function

    For testing: Change the code so it does NOT call setVisible() if it has called dispose(). You could use a static variable for that.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    Quote Originally Posted by Norm View Post
    For testing: Change the code so it does NOT call setVisible() if it has called dispose(). You could use a static variable for that.
    Well I couldn't understand your response. I am sure that setVisible() is not called after dispose().
    Which variable should I declare static??

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: JFrame not responding to dispose() function

    I am sure that setVisible() is not called after dispose().
    To see if that is true, add a call to println() just after the call to dispose() and another after the call to setVisible().
    Execute the code and see what is printed.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    amitSingh is offline Member
    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0

    Default Re: JFrame not responding to dispose() function

    Quote Originally Posted by Norm View Post
    To see if that is true, add a call to println() just after the call to dispose() and another after the call to setVisible().
    Execute the code and see what is printed.
    I tried it and it turned out you were right, the constructor IS called after dispose().
    So I tried sopln() in different ways and learned that main function from where the base class is called is executed even after the constructor.
    So I modified the code and finally solved the problem

    Java Code:
    import javax.swing.JFileChooser;
    public class Test1 {
        public static void main(String[] args) {
            base obj=new base();
            int ret=new JFileChooser().showOpenDialog(obj);
            if(ret==JFileChooser.CANCEL_OPTION)
            {
                obj.dispose();
                System.out.println("after dispose()");
            }
            else
                obj.setVisible(true);
            System.out.println("in the Main class");
        }
    }
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.UnsupportedLookAndFeelException;
    public final class base extends javax.swing.JFrame {
            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);
            System.out.println("in the constructor");
        }
    //not including initComponents()
    }
    The output it gives :

    in the constructor
    after dispose()
    in the Main class

    Though I still don't understand what exactly caused the problem.
    Last edited by amitSingh; 06-14-2014 at 07:22 AM.

Similar Threads

  1. KeyListener isn't responding when JFrame isn't focused
    By totalspelnerd in forum AWT / Swing
    Replies: 7
    Last Post: 07-01-2013, 04:45 PM
  2. Replies: 3
    Last Post: 08-12-2012, 09:32 AM
  3. Help with JFrame function!!!!
    By achiu17 in forum New To Java
    Replies: 3
    Last Post: 11-08-2011, 05:45 AM
  4. Dispose Jframe from another class.
    By vahshir in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 12:33 PM
  5. i cant dispose the jframe(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 2
    Last Post: 02-24-2009, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •