Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JOptionPane preventing program from ending

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JOptionPane preventing program from ending

    I wanted to use a JOptionPane to display a simple message. And it all appears to work fine. But the program never ends. Its not stuck in any obvious loops or similar.

    See the simple program below that demonstates the problem

    import javax.swing.*;
     
    public class MainScreen {
     
     
        public static void main(String[] args) {
     
     
     
            System.out.println("begin");
            JFrame frame = new JFrame("ERROR");
            JOptionPane.showMessageDialog(frame,"This is a message");
            System.out.println("end");
        }
    }

    From the System.out.println statements it can be seen that after the ok button is pressed it runs to the end. But the program doesn't exit. Im running it in the Netbeans IDE if that makes any difference

    The only solution i've found on the internet is to put system.exit(0) at the end of the code. But this seems like it would be bad practice. Leaving some remnant of the JOptionPane hanging around clogging up memory then forcing it to close. Is this in fact how its supposed to be done?

    Thanks for any help (I presume i'm missing a simple instruction)


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: JOptionPane preventing program from ending

    You have two things going on here.

    1) JOptionPane.showMessageDialog(frame,"This is a message");

    2) JFrame frame = new JFrame("ERROR");

    Its been a while since I have played with JOptionPane, but it was my understanding it can stand alone with out JFrame.

    add these lines to your code.
    a) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    b) frame.setVisible(true);

    a. will tell your program to finish running with a standard click on the red x in the JFrame (note this is different from your JOptionPane
    b. will make the JFrame visible so you can even see and click the red x.

    it may be just as easy to do this to JOptionPane, but again I haven't played with it in years.

    EDIT: it seems JOptionPane does this automatically, The only reason your program didn't close is because of the invisible JFrame. If you dont have a use for the JFrame try this:

    import javax.swing.*;
     
    public class MainScreen {
     
     
        public static void main(String[] args) {
     
            System.out.println("begin");
            JOptionPane.showMessageDialog(null,"This is a message");
            System.out.println("end");
        }
    }

    I removed creating a JFrame all together, and used null to fill the parameter in the method call.

    Hope this is what you needed,
    Jonathan
    Last edited by JonLane; February 23rd, 2012 at 07:47 AM.

  3. The Following User Says Thank You to JonLane For This Useful Post:

    RichTea (February 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane preventing program from ending

    Thanks JonLane!

    That makes a lot of sense now, I thought I was creating a frame for the JOptionPane to live in but now I see they're seperate and have to be closed seperately.

    For anyone finding this i've now figured out you can create a JOptionPane that lives alone using:
    JOptionPane.showMessageDialog(null, "This is a message");

    This does allow the program to end because there is no secret frame hanging about

Similar Threads

  1. [SOLVED] Help needed writing a program using JOptionPane
    By s1mmi in forum Object Oriented Programming
    Replies: 3
    Last Post: January 30th, 2012, 05:39 PM
  2. [SOLVED] Preventing a JFrame from Being Iconified
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2011, 01:31 PM
  3. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM
  4. Replies: 1
    Last Post: November 9th, 2010, 09:58 AM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM

Tags for this Thread