0

I import javax.swing.*; and have a JOptionPane:

String meny = JOptionPane.showInputDialog(null, "What's the answer?").trim();

I want this window to close after the amount of seconds the user inserts:

String time = JOptionPane.showInputDialog(null, "How much time").trim();

int timer1 = Integer.parseInt(time);

I do not know how to do this. Should I do something like this?

if (timer1 == 0) {
meny.setVisible(false);
}

Thanks in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Habbo
  • 155
  • 1
  • 8
  • 2
    It's "complicated", have a look at [this for an example](http://stackoverflow.com/questions/22979504/closing-a-runnable-joptionpane/22979571#22979571) – MadProgrammer Sep 17 '15 at 11:52

1 Answers1

0

Try this code:

            JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
            JDialog dialog = pane.createDialog(null, "Title");
            dialog.setModal(false);
            dialog.setVisible(true);

            new Timer(10000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                }
            }).start();
Vaseph
  • 704
  • 1
  • 8
  • 20