-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageBox.java
95 lines (85 loc) · 3.54 KB
/
MessageBox.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class MessageBox extends JTextPane {
private static final long serialVersionUID = 3760312257620803477L;
private StyledDocument doc;
private Style style;
public MessageBox() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBackground(Color.white);
this.doc = this.getStyledDocument();
this.style = this.addStyle("My Style", null);
this.setEditable(false);
this.setEditorKit(new WrapEditorKit());
this.setContentType("text/html");
}
public void addMessage(String message) {
String fontFamily = this.getFont().getFamily();
int fontSize = this.getFont().getSize();
this.setText("<html><body style=\"font-family:" + fontFamily + ";font-size:" + fontSize + "\"><span style=\"color:black\">" + message + "\n</span><br>" + this.getText());
/*
StyleConstants.setForeground(style, Color.black);
try {
doc.insertString(0, message + "\n", style);
} catch (BadLocationException e) {
e.printStackTrace();
}*/
this.revalidate();
}
public void addMessage(String message, Color color) {
String fontFamily = this.getFont().getFamily();
int fontSize = this.getFont().getSize();
this.setText("<html><body style=\"font-family:" + fontFamily + ";font-size:" + fontSize + "\"><span style=\"color:red\">" + message + "\n</span><br>" + this.getText());
/*
StyleConstants.setForeground(style, color);
try {
doc.insertString(0, "<html><b>" + message + "</b></html>\n", style);
} catch (BadLocationException e) {
e.printStackTrace();
}*/
this.revalidate();
}
class WrapEditorKit extends StyledEditorKit {
private static final long serialVersionUID = 2002232136754917832L;
ViewFactory defaultFactory=new WrapColumnFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
class WrapColumnFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new WrapLabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
class WrapLabelView extends LabelView {
public WrapLabelView(Element elem) {
super(elem);
}
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
}