-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToastMessage.java
44 lines (38 loc) · 1.41 KB
/
ToastMessage.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
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class ToastMessage extends JDialog {
int miliseconds;
private static final long serialVersionUID = 1L;
public ToastMessage(String toastString, int time) {
this.miliseconds = time;
setUndecorated(true);
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
panel.setBorder(new LineBorder(Color.LIGHT_GRAY, 2));
getContentPane().add(panel, BorderLayout.CENTER);
JLabel toastLabel = new JLabel("");
toastLabel.setText(toastString);
toastLabel.setFont(new Font("Dialog", Font.BOLD, 12));
toastLabel.setForeground(Color.WHITE);
setBounds(100, 100, toastLabel.getPreferredSize().width+20, 31);
setAlwaysOnTop(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int y = dim.height/2-getSize().height/2;
int half = y/2;
setLocation(dim.width/2-getSize().width/2, y+half);
panel.add(toastLabel);
setVisible(false);
new Thread(){
public void run() {
try {
Thread.sleep(miliseconds);
dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}