Guest User

Untitled

a guest
Jan 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public final class AutoCloseConfirmDialog {
  5.  
  6. private AutoCloseConfirmDialog(){
  7.  
  8. }
  9.  
  10. public static boolean showAutoCloseConfirmDialog(Component parent, String title, String message, int countDown) {
  11. final JProgressBar progressBar = new JProgressBar(0, countDown);
  12.  
  13. Object[] body = {message, progressBar};
  14.  
  15. final JOptionPane pane = new JOptionPane(body, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
  16. final JDialog dialog = pane.createDialog(parent, title);
  17.  
  18. progressBar.setPreferredSize(new Dimension(175, 20));
  19. progressBar.setValue(countDown);
  20.  
  21. dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
  22.  
  23. final AutoCloseDialogTask task = new AutoCloseDialogTask(progressBar, countDown);
  24.  
  25. task.addPropertyChangeListener(evt -> {
  26. if ("progress".equals(evt.getPropertyName())) {
  27. if (task.isDone()) {
  28. pane.setValue(JOptionPane.YES_OPTION);
  29. dialog.setVisible(false);
  30. dialog.dispose();
  31. }
  32. }
  33. });
  34.  
  35. task.execute();
  36. dialog.setVisible(true);
  37.  
  38. return (int) pane.getValue() == JOptionPane.YES_OPTION;
  39. }
  40.  
  41. private static class AutoCloseDialogTask extends SwingWorker<Void, Void> {
  42.  
  43. private JProgressBar progressBar;
  44. private int limit;
  45.  
  46. AutoCloseDialogTask(JProgressBar bar, int countDown) {
  47. progressBar = bar;
  48. limit = countDown;
  49. }
  50.  
  51. @Override
  52. public Void doInBackground() {
  53. int progress = limit;
  54. //Initialize progress property.
  55. setProgress(0);
  56. while (progress > 0) {
  57. //Sleep for up to one second.
  58. try {
  59. Thread.sleep(1000);
  60. } catch (InterruptedException ignore) {
  61. }
  62. progress--;
  63. setProgress(progress);
  64. progressBar.setValue(progress);
  65. }
  66. setProgress(100);
  67. return null;
  68. }
  69.  
  70. @Override
  71. public void done() {
  72.  
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment