By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,608 Members | 3,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,608 IT Pros & Developers. It's quick & easy.

Java Swing Application Reload?

risk32
P: 98
Hi all. I have a really confusing problem. I'm using Swing and I'm trying to do a confirmation box :
Expand|Select|Wrap|Line Numbers
  1. int reply;
  2. String message = "Do you want to input another number?";
  3. String title = "Input Another Number?";
  4. reply = (JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
  5. if (reply == JOptionPane.NO_OPTION) {
  6. System.exit(0); }
  7.  
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.

Expand|Select|Wrap|Line Numbers
  1. // TimeConverter.java
  2. // Author: Adam Martin 
  3. // Assignment #2, Time Converter 
  4. // This program lets the user input seconds and converts into hours, minutes and seconds (HH:MM:SS).
  5.  
  6.     // Imports the GUI interface used by Swing. JOptionPane is used for producing special windows called dialog 
  7.     // windows, dialog boxes, or just dialogs.
  8.     import javax.swing.JOptionPane;
  9.  
  10.      public class TimeConverter
  11.         {
  12.             public static void main(String[] args)
  13.                 {            
  14.  
  15.  
  16.     // Input dialog box to input number of seconds. Takes the input from the user and assigns it to a string
  17.     // and converts the string into an integer
  18.  
  19.        String secondsString = JOptionPane.showInputDialog("Enter number of seconds:");
  20.     int secondsAmount = Integer.parseInt(secondsString);                    
  21.  
  22.     int hours, minutes, seconds, remainder;  // whole number for hours, divides seconds by 3600 (number of seconds in one hour)    
  23.         hours = secondsAmount / 3600;        // the remainder operator (%) will get the remainder of secondsAmount / 3600 
  24.         remainder = secondsAmount % 3600;    // whole number for minutes, divides the remainder by 60 (number of seconds in one minute)
  25.         minutes = remainder / 60;            // remainder operator gets the remainder of secondsAmount / 60, it will be used as the left
  26.         seconds = remainder % 60;            // over seconds that were not converted
  27.  
  28.         JOptionPane.showMessageDialog(null,"The time is " + (hours < 10 ? "0" : "") + hours + ":" +
  29.                                      (minutes < 10 ? "0" : "") + minutes + ":" + (seconds <10 ? "0" : "") + seconds + " (HH:MM:SS)");
  30.  
  31.     int reply;
  32.         String message = "Do you want to input another number?"; 
  33.         String title = "Input Another Number?";  
  34.         reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
  35.            if (reply == JOptionPane.NO_OPTION) {  
  36.         System.exit(0); }
  37.         if (reply == JOptionPane.YES_OPTION) {
  38.                     }
  39.                 }
  40.             }                    
  41.  
  42.  
Thanks,
Adam
Dec 10 '08 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Just a while loop will do:

Expand|Select|Wrap|Line Numbers
  1. while (true) {
  2.    // all your original code here ...
  3.    // ...
  4.    if (JOptionPane.showConfirmDialog(null, message, title,  
  5.          JOptionPane.YES_NO_OPTION == JOptionPane.NO_OPTION) 
  6.         System.exit(0);
  7. }
  8.  
kind regards,

Jos
Dec 10 '08 #2

risk32
P: 98
That's all there is to it? What determines the true/false value of the code?
Dec 11 '08 #3

10K+
P: 13,264
Read the code that has been posted and see what that if is doing there.
Dec 11 '08 #4

risk32
P: 98
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.
Dec 12 '08 #5

Expert 10K+
P: 11,448
@risk32
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:

Expand|Select|Wrap|Line Numbers
  1. boolean result= ... something complicated ...
  2. if (result) ...
  3.  
and I did this:

Expand|Select|Wrap|Line Numbers
  1. if (... something complicated ...) ...
  2.  
kind regards,

Jos
Dec 12 '08 #6

karthickkuchanur
100+
P: 156
@JosAH
I dont know where to post the new thread

iam login after a long time ,iam ashame to post here
Dec 12 '08 #7

10K+
P: 13,264
Go to answers - bytes
There is a button for "New Thread" near the top left corner.
Dec 12 '08 #8

risk32
P: 98
Thanks for the help Jos, I had to tweak my code a little since I had 2 showConfirmDialog 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
Dec 17 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.