Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

java - Timer or other idea required to allow code to continue execution after calling method and JOptionPane

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 + ".
Current humidity: " + w.currentHumidity + 
            "%.
Chance of precipitation: " + w.chancePrecip + "%.", "Weather Update: " + w.location.zipCode, JOptionPane.INFORMATION_MESSAGE, icon);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...