0

When I try to get my JDialog containing the correct info (fed in through my custom object WeatherCard) to display, it disappears an instant after creation. Did I miss something or did I just fudge it too hard?

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);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0, 1));
    Label lb = new Label("It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity);
    panel.add(lb);
    final JDialog dialog = new JDialog(controlFrame, "Weather Update: " + w.location.zipCode);
    dialog.setSize(500,500);
    dialog.add(panel);
    dialog.pack();
    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    timer.setRepeats(false);
    timer.start();

    dialog.setVisible(true);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nick
  • 191
  • 1
  • 2
  • 9
  • hmm this sounds weird, can you print timestamp right after timer.start() and at the beginning of actionPerformed? you could also try to print the initial delay by timer.getInitialDelay() – JohnnyAW Sep 25 '13 at 09:06

1 Answers1

2

Your approach works in this complete example below. Your result may be an artifact of neglecting to construct the GUI on the on the event dispatch thread. Also, start() the Timer after the dialog is visible.

plain image

Also consider this example that displays the time remaining.

timed image

import java.awt.EventQueue;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/19003038/230513
 */
public class Test {

    static class TestAction extends AbstractAction {

        private final JFrame f;

        public TestAction(String name, JFrame f) {
            super(name);
            this.f = f;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JPanel panel = new JPanel();
            Label label = new Label("It is currently sunny & warm somewhere.");
            panel.add(label);
            final JDialog dialog = new JDialog(f, "Weather Update");
            dialog.add(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(f);
            dialog.setVisible(true);
            Timer timer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                    dialog.dispose();
                }
            });
            timer.setRepeats(false);
            timer.start();
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JButton(new TestAction("Test", f)));
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • When I get home from work, I'll give your ideas a go. The concept is it waits half an hour, then when Thread.sleep() is done with its sleep, it will then pop up a JDialog with the info. Then, hopefully, it'll close after a few seconds. – Nick Sep 25 '13 at 11:54