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
|
public class MessagePanel extends JPanel {
public MessagePanel() {
super();
messageOut = new JTextPane();
docOut = new DefaultStyledDocument();
setLayout(new BorderLayout());
messageOut.setEditable(false);
messageOut.setPreferredSize(new Dimension(100,100));
messageOut.setDocument(docOut);
def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
StyleConstants.setFontFamily(def, "SansSerif");
regular = messageOut.addStyle("regular", def);
s = messageOut.addStyle("red", regular);
StyleConstants.setForeground(s, Color.RED);
s = messageOut.addStyle("green", regular);
StyleConstants.setForeground(s, Color.GREEN);
s = messageOut.addStyle("black", regular);
StyleConstants.setForeground(s, Color.BLACK);
sp = new JScrollPane(messageOut);
add(new JLabel(" MESSAGE :"), BorderLayout.NORTH);
add(sp, BorderLayout.CENTER);
}
public static void writeMessageInWindows(String text, String color) {
try {
String timeNow = DateFormat.getTimeInstance(DateFormat.SHORT,
Locale.FRANCE).format(new Date());
docOut.insertString(docOut.getLength(), "<" + timeNow + "> ",
messageOut.getStyle("black"));
docOut.insertString(docOut.getLength(), text, messageOut
.getStyle(color));
docOut.insertString(docOut.getLength(), "\n", messageOut
.getStyle("black"));
docOut.addDocumentListener(new OutDocListener());
} catch (BadLocationException ble) {
System.out.println("ERREUR ECRITURE MESSAGES");
}
}
public static void writeMessageInWindows(String text) {
writeMessageInWindows(text, "black");
}
static JTextPane messageOut;
private static DefaultStyledDocument docOut;
private Style def;
private Style regular;
private Style s;
private static final long serialVersionUID = 1L;
static JScrollPane sp;
}
//////////////////////////////////////
//////////////////////////////////////
class OutDocListener implements DocumentListener {
public void insertUpdate(DocumentEvent arg0) {
JTextPane text = MessagePanel.messageOut;
text.setCaretPosition(text.getDocument().getLength());
}
public void removeUpdate(DocumentEvent arg0) {
}
public void changedUpdate(DocumentEvent arg0) {
}
} |
Partager