Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

clearing JOptionPane problem


Hi all,

I'm new here and new to java programming. I have a background in C++.

My question concerns using JOptionPane.showInputDialog twice in a program.

I use it once with some text and then use it again inside a for loop with different text. It keeps the text from the first use instead of changing it when I run the program.

import javax.swing.JOptionPane;


public class Largest {

    public static void main(String[] args) {
   
   
    String num;
    int num1 = 0;
    int largest = 0;
   
   
   
   
    num = JOptionPane.showInputDialog(null, "Enter 10 numbers one at a time clicking 'OK' each time");
    num1 = Integer.parseInt(num);
   
   
   
    for(int i = 0; i < 9; i++)
    {
    //input number
    num =  JOptionPane.showInputDialog(null, "NEXT NUMBER");
    num1 = Integer.parseInt(num);
    if (num1 > largest)
    largest = num1;
      
   
    }
   
   
    JOptionPane.showMessageDialog(null, "The largest number is " + largest,
    JOptionPane.INFORMATION_MESSAGE);
   
    System.exit(0);
   
       
   
    }
   
   
}

The program works fine but after the first input dialog the text should change and it doesn't. I've tried using a different variable for the first one but it made no differenc. The second one still used the text from the first one while it's inside the loop.

Can anyone tell me what I'm doing wrong here?

Thanks

Your rating: None

Play with Swing in the Event Dispatch Thread

Your code won't compile. There is not a JOptionPane.showMessageDialog method that accepts three parameters.

After correcting the compile error, I was able to run your program, and did in fact see the input dialog message change as you intended. My Java Runtime environment consists of Sun's JRE 6 update 16 installed on Windows XP.

Your problem might be a timing issue, because your code uses Swing classes outside the Event Dispatch thread (EDT). Code that interacts directly with classes in the Swing API should be executed in the EDT. This can be accomplished by using javax.swing.SwingUtilities.invokeAndWait.

Here's a modified version of your code that addresses the points above.

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class JOptionPaneTest {

public final static int INPUT_COUNT = 10;

public static void main(String[] arg) {
    try {
        SwingUtilities.invokeAndWait( new NumberInputWork() );
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    catch (InvocationTargetException e) {
        throw new RuntimeException( e );
    }
}

private static class NumberInputWork implements Runnable {

    public void run() {
        int largest = Integer.MIN_VALUE;

        for (int i = 1; i <= INPUT_COUNT; i++) {
            String message = MessageFormat.format(
                    "Enter a number ({0} of {1}).",
                    i,
                    INPUT_COUNT );
            String num = JOptionPane.showInputDialog( null, message );
            largest = Math.max( largest, Integer.parseInt( num ) );
        }

        String message = MessageFormat.format(
                "The largest number entered was {0}.",
                largest );

        JOptionPane.showMessageDialog( null,
                message,
                "Result",
                JOptionPane.INFORMATION_MESSAGE );
    }
}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.