使用计时器自动关闭JOptionPane.showconfirmDialog

时间:2015-05-15 05:08:00

标签: java swing timer joptionpane

我想创建一个带有计时器的JOptionPane.showConfirmDialog。默认选项为Exit。但是,如果我单击是选项它应该继续工作,如果我单击否选项它应该退出。如果我没有点击任何选项,它应该自动退出代码。

我尝试了下面的示例代码。它部分工作。但问题是我无法模拟是/否选项。在任何情况下,它都会从带有YES选项的代码中退出。

虽然这段代码取自其中一个线程,但实现方式却有所不同。我只是根据我的需要修改了代码。 请找到以下代码:

public class TestProgress {

public static void main(String[] args) {
    final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    final JDialog dlg = msg.createDialog("Select Yes or No");
    final int n = msg.YES_NO_OPTION;
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              Thread.sleep(5000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }

            if(msg.YES_OPTION==n){
                System.out.println("Continue the work.. "); // should not exit
            }
            else if(msg.NO_OPTION==n)
                dlg.setVisible(false);
                System.exit(1);
            }


        }).start();
        dlg.setVisible(true);
        System.out.println("Outside code.");
    }
}  

我还需要做些什么才能让它正常工作?

2 个答案:

答案 0 :(得分:2)

JOptionPane的自动关闭对话框

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class TestProgress {

    public static void main(String[] args) {


        final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
        final JDialog dlg = msg.createDialog("Select Yes or No");
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dlg.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                super.componentShown(e);
                final Timer t = new Timer(5000,new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dlg.setVisible(false);
                    }
                });
                t.start();
            }
        });
        dlg.setVisible(true);
        System.out.println("Outside code.");
    }

}  

更新:

使用扩展构造函数,您可以在其中传递初始选项并将NO指定为默认值

JOptionPane(Object message, int messageType, int optionType,
                   Icon icon, Object[] options, Object initialValue)

答案 1 :(得分:1)

此问题的完整答案。

public final static boolean showConfirmDialogWithTimeout(Object params, String title, int timeout_ms) {
    final JOptionPane msg = new JOptionPane(params, JOptionPane.WARNING_MESSAGE, JOptionPane.CANCEL_OPTION);
    final JDialog dlg = msg.createDialog(title);

    msg.setInitialSelectionValue(JOptionPane.OK_OPTION);
    dlg.setAlwaysOnTop(true);
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
            super.componentShown(e);
            final Timer t = new Timer(timeout_ms, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dlg.setVisible(false);
                }

            });
            t.start();
        }
    });
    dlg.setVisible(true);

    Object selectedvalue = msg.getValue();
    if (selectedvalue.equals(JOptionPane.CANCEL_OPTION)) {
        return false;
    } else {
        return true;
    }
}

    // example usage
    String message = "The earth will explode in 10 seconds. Select CANCEL if you won't.";
    JLabel lbmsg = new JLabel(message);
    boolean result = showConfirmDialogWithTimeout(lbmsg, "Shutdown Warning", 10 * 1000);

    if (result == false) {
        Utils.showMessage(message + " cancel is selected");
    }
    else {
        Utils.showMessage(message + " timeout or okay is selected");
    }