1 package org.wcb.gui.component;
2
3 import javax.swing.*;
4 import javax.swing.text.*;
5 import java.awt.*;
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 public class JMessageTextPane {
34
35 private static final String NEWLINE = "\n";
36
37 private String[] stringInit;
38 private Color backgroundColor = new Color(0xefd67a);
39 private JTextPane textPane;
40
41 public JMessageTextPane() {
42 textPane = new JTextPane();
43 }
44
45 public JMessageTextPane(String[] textString) {
46 this.stringInit = textString;
47 textPane = new JTextPane();
48 }
49
50 public void setBackground(Color color) {
51 this.backgroundColor = color;
52 }
53
54 public JTextPane getTextPane() {
55 textPane.setBackground(this.backgroundColor);
56 StyledDocument doc = textPane.getStyledDocument();
57 Style def = StyleContext.getDefaultStyleContext().
58 getStyle(StyleContext.DEFAULT_STYLE);
59 doc.addStyle("regular", def);
60 StyleConstants.setFontFamily(def, "SansSerif");
61
62
63 try {
64 for (String aStringInit : stringInit) {
65 doc.insertString(doc.getLength(), aStringInit + NEWLINE,
66 doc.getStyle("regular"));
67 }
68 } catch (BadLocationException ble) {
69 System.err.println("Bad document error in JMessageTextPane " + ble);
70 }
71 textPane.setEditable(false);
72 return textPane;
73 }
74
75 public JTextPane createTextPane(String[] initString) {
76 textPane.setBackground(this.backgroundColor);
77 StyledDocument doc = textPane.getStyledDocument();
78 Style def = StyleContext.getDefaultStyleContext().
79 getStyle(StyleContext.DEFAULT_STYLE);
80 doc.addStyle("regular", def);
81 StyleConstants.setFontFamily(def, "SansSerif");
82
83
84 try {
85 for (int i=0; i < initString.length; i++) {
86 doc.insertString(doc.getLength(), initString[i] + NEWLINE,
87 doc.getStyle("regular"));
88 }
89 } catch (BadLocationException ble) {
90 System.err.println("Bad document error in JMessageTextPane " + ble);
91 }
92 textPane.setEditable(false);
93 return textPane;
94 }
95 }