Java Swing Application Reload?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • risk32
    New Member
    • Mar 2008
    • 98

    Java Swing Application Reload?

    Hi all. I have a really confusing problem. I'm using Swing and I'm trying to do a confirmation box :
    Code:
    int reply;
    String message = "Do you want to input another number?";
    String title = "Input Another Number?";
    reply = (JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
    if (reply == JOptionPane.NO_OPTION) {
    System.exit(0); }
    This part works great, now down to business:
    I'm trying to get the program to basically reload it, or go back to the beginning
    if the reply is YES.
    I have googled until my fingers hurt, but I haven't found anything except for JS.
    (The window.reload function)

    Here's the complete code for my program. If there's anything I can do to accomplish this, please let me know. Otherwise I'll be forced to take it out of the program. By the way, I'm using JCreator.

    Code:
    // TimeConverter.java
    // Author: Adam Martin 
    // Assignment #2, Time Converter 
    // This program lets the user input seconds and converts into hours, minutes and seconds (HH:MM:SS).
    	
    	// Imports the GUI interface used by Swing. JOptionPane is used for producing special windows called dialog 
    	// windows, dialog boxes, or just dialogs.
    	import javax.swing.JOptionPane;
    	  
     	public class TimeConverter
    		{
    			public static void main(String[] args)
    				{			
    					
    					
    	// Input dialog box to input number of seconds. Takes the input from the user and assigns it to a string
    	// and converts the string into an integer
       										
       	String secondsString = JOptionPane.showInputDialog("Enter number of seconds:");
    	int secondsAmount = Integer.parseInt(secondsString);					
    			
    	int hours, minutes, seconds, remainder;  // whole number for hours, divides seconds by 3600 (number of seconds in one hour)	
    		hours = secondsAmount / 3600;		// the remainder operator (%) will get the remainder of secondsAmount / 3600 
    		remainder = secondsAmount % 3600;	// whole number for minutes, divides the remainder by 60 (number of seconds in one minute)
    		minutes = remainder / 60;			// remainder operator gets the remainder of secondsAmount / 60, it will be used as the left
    		seconds = remainder % 60;			// over seconds that were not converted
    						
    		JOptionPane.showMessageDialog(null,"The time is " + (hours < 10 ? "0" : "") + hours + ":" +
    									 (minutes < 10 ? "0" : "") + minutes + ":" + (seconds <10 ? "0" : "") + seconds + " (HH:MM:SS)");
       							
    	int reply;
    		String message = "Do you want to input another number?"; 
    		String title = "Input Another Number?";  
    		reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
       		if (reply == JOptionPane.NO_OPTION) {  
    		System.exit(0); }
    		if (reply == JOptionPane.YES_OPTION) {
    					}
    				}
    			}
    Thanks,
    Adam
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Just a while loop will do:

    Code:
    while (true) {
       // all your original code here ...
       // ...
       if (JOptionPane.showConfirmDialog(null, message, title,  
             JOptionPane.YES_NO_OPTION == JOptionPane.NO_OPTION) 
            System.exit(0);
    }
    kind regards,

    Jos

    Comment

    • risk32
      New Member
      • Mar 2008
      • 98

      #3
      That's all there is to it? What determines the true/false value of the code?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Read the code that has been posted and see what that if is doing there.

        Comment

        • risk32
          New Member
          • Mar 2008
          • 98

          #5
          The if statement is if the JOptionPane.NO_ OPTION is selected, the system will exit. BUT, there is no statement for the boolean value of true/false for the option selected.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by risk32
            The if statement is if the JOptionPane.NO_ OPTION is selected, the system will exit. BUT, there is no statement for the boolean value of true/false for the option selected.
            You don't need it, it is directly put in the if clause. Anything that can be true or false can be used for an if clause;

            You did this:

            Code:
            boolean result= ... something complicated ...
            if (result) ...
            and I did this:

            Code:
            if (... something complicated ...) ...
            kind regards,

            Jos

            Comment

            • karthickkuchanur
              New Member
              • Dec 2007
              • 156

              #7
              I dont know where to post the new thread

              Originally posted by JosAH
              Just a while loop will do:

              Code:
              while (true) {
                 // all your original code here ...
                 // ...
                 if (JOptionPane.showConfirmDialog(null, message, title,  
                       JOptionPane.YES_NO_OPTION == JOptionPane.NO_OPTION) 
                      System.exit(0);
              }
              kind regards,

              Jos
              I dont know where to post the new thread

              iam login after a long time ,iam ashame to post here

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Go to answers - bytes
                There is a button for "New Thread" near the top left corner.

                Comment

                • risk32
                  New Member
                  • Mar 2008
                  • 98

                  #9
                  Thanks for the help Jos, I had to tweak my code a little since I had 2 showConfirmDial og screens. I also had to add an extra ')' that was missing. I found that out from my Java instructor. I must say, it's conforting that there are individuals like yourselves willing to help others when learning, or when they're stuck. I really appreciate it.

                  Adam

                  Comment

                  Working...