-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeOutOptionPane.java
executable file
·53 lines (46 loc) · 1.76 KB
/
TimeOutOptionPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @author Steven Sams
* Dialog with timeout on 'OK' button to prevent blocking interface
*/
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class TimeOutOptionPane {
final static int PRESET_TIME = 15;
private TimeOutOptionPane() {
}
public static void showMessageDialog(Component parentComponent, Object message, String title, int optionType, int messageType) {
JOptionPane pane = new JOptionPane(message, messageType, optionType, null);
pane.setMessageType(messageType);
JPanel buttonPanel = (JPanel) pane.getComponent(1);
final JButton buttonOk = (JButton) buttonPanel.getComponent(0);
final JDialog dialog = pane.createDialog(parentComponent, title);
buttonOk.setText("OK [" + PRESET_TIME + "]");
new Thread() {
@Override
public void run() {
try {
for (int i = PRESET_TIME; i >= 0; i--) {
Thread.sleep(1000);
if (dialog.isVisible() && i < PRESET_TIME) {
buttonOk.setText("OK [" + i + "]");
}
}
if (dialog.isVisible()) {
dialog.setVisible(false);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
dialog.dispose();
}
}
}.start();
dialog.setVisible(true);
}
public static void main(String args[]) {
TimeOutOptionPane.showMessageDialog(null, "Message body", "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
}