簡體   English   中英

使用計時器自動關閉JOptionPane.showconfirmDialog

[英]Autoclose JOptionPane.showconfirmDialog with a timer

我想創建一個帶有計時器的JOptionPane.showConfirmDialog 默認選項是退出。 但是,如果我單擊“是”選項,它應該繼續工作,如果我單擊“否選項”,它應該退出。 如果我不單擊任何選項,它將自動從代碼中退出。

我嘗試了以下示例代碼。 它正在部分工作。 但是問題是我無法模擬“是/否”選項。 無論如何,它都是使用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.");
    }
}  

我還需要做什么才能使其正常工作?

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)

這個問題的完整答案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM