2

I need a way to allow my program to keep running code after this method is called.

Currently, it waits for half an hour, gets the info, stores it to the object WeatherCard, and displays it, and repeats. But it hangs on the JOptionPane. I need a way to make it so that the program either keeps going underneath the JOptionPane or to close the pane after about 10 seconds. I am not sure how to work either into my code, currently

public void printWeatherCard(WeatherCard w, JFrame controlFrame) throws MalformedURLException, IOException{
    /* Displays a dialog box containing the temperature and location */
    BufferedImage img = ImageIO.read(new URL(w.imgSrc));
    ImageIcon icon = new ImageIcon(img);

    JOptionPane.showMessageDialog(controlFrame, "It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity + 
            "%.\nChance of precipitation: " + w.chancePrecip + "%.", "Weather Update: " + w.location.zipCode, JOptionPane.INFORMATION_MESSAGE, icon);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nick
  • 191
  • 1
  • 2
  • 9

3 Answers3

3

Closing a modal dialog after some delay and updating the display behind a modal dialog are distinct issues.

  • In this example, a javax.swing.Timer is used to mark time, and the dialog is closed when a counter reaches zero or the user dismisses it.

  • A modal dialog only blocks user interaction. Add a modal dialog to this example to see that GUI updates continue in response to the javax.swing.Timer.

    public void run() {
        ...
        f.setVisible(true);
        JOptionPane.showMessageDialog(dt, TITLE);
    }
    
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

But it hangs on the JOptionPane. I need a way to make it so that the program either keeps going underneath the JOptionPane or to close the pane after about 10 seconds. I am not sure how to work either into my code, currently

there are two ways

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I decided to go with a custom JDialog (thought it will require quite a bit of reworking things in my program). Thank you very much! – Nick Sep 11 '13 at 03:47
0

JOptionPane is modal, which means code execution blocks until the user dismisses or acknowledges it. You need to use a non-modal dialog. Consider creating your own JDialog. http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
  • A modal dialog only blocks _user_ interaction, not execution. – trashgod Sep 10 '13 at 21:03
  • @trashgod - It does block execution on the current thread. In the code Nick posted his program was waiting on the JOptionPane, which halted the execution of his code. – Dodd10x Sep 11 '13 at 15:18
  • Place a modal dialog before the setVisible on the Jframe, inside the random bubbdle method, or inside the action listener code all halt execution of your example. If you place it in the paintComponent code you'll see repeated popups but the paintComponent code is halted each time. In each instance the current thread is waiting on input and will not execute code until the user acknowledges the modal dialog. I suspect that placing it after the setVisible does not block because at that point the timer is already running and the RepaintManager is intercepting all of the repaint calls. – Dodd10x Sep 11 '13 at 16:05
  • It's certainly _possible_ to block the EDT, but the `JOptionPane` itself doesn't do so. Sorry if I'm missing your point. I suspect that @Nick's "custom `JDialog`" means a _modeless_ dialog, for [example](http://stackoverflow.com/a/11832979/230513). – trashgod Sep 11 '13 at 17:37