3 * http://www.robodesign.ro
5 * $Date: 2010-05-19 14:00:29 +0300 $
10 import java
.awt
.Color
;
11 import java
.awt
.Rectangle
;
12 import java
.io
.FileReader
;
13 import javax
.swing
.JEditorPane
;
14 import javax
.swing
.JScrollPane
;
15 import javax
.swing
.text
.Element
;
16 import javax
.swing
.text
.html
.HTMLDocument
;
17 import javax
.swing
.text
.html
.StyleSheet
;
19 // clasa ce permite utilizarea facila a unui JEditorPane in taburile ce afiseaza
21 public class PanouMesaje
extends JScrollPane
{
22 private HTMLDocument myDoc
= null;
23 private Element myElemMesaje
= null;
24 private JEditorPane myEditorPane
= null;
25 private final String myFisierStil
= "stil.css";
27 // initializarea sablon a unui JEditorPane
28 public PanouMesaje () {
31 myEditorPane
= new JEditorPane();
32 myEditorPane
.setOpaque(true);
33 myEditorPane
.setBackground(Color
.WHITE
);
34 myEditorPane
.setEditable(false);
35 myEditorPane
.setContentType("text/html; charset=utf8");
36 myEditorPane
.setText("<html><head><meta charset='utf8'></head><body id='mesaje'></body></html>");
38 myDoc
= (HTMLDocument
) myEditorPane
.getDocument();
39 myElemMesaje
= myDoc
.getElement("mesaje");
40 StyleSheet style
= myDoc
.getStyleSheet();
43 style
.loadRules(new FileReader(myFisierStil
), null);
44 } catch (Exception ex
) {
45 System
.err
.println("Nu am reusit sa aplic stilul la afisarea de mesaje. " + ex
);
49 this.setViewportView(myEditorPane
);
52 public JEditorPane
getMyEditorPane () {
56 // metoda ce permite adaugarea rapida a unui mesaj
57 public void adaugaMesaj (String htmlStr
, String cssClass
) {
58 Element ultimulElem
= myElemMesaje
.getElement(myElemMesaje
.getElementCount()-1);
61 myDoc
.insertAfterEnd(ultimulElem
, "<p class='" + cssClass
+ "'>" + htmlStr
+ "</p>\n");
62 } catch (Exception ex
) {
63 System
.err
.println("Afisarea de mesaje a esuat. " + ex
);
68 myEditorPane
.scrollRectToVisible(new Rectangle(0, myEditorPane
.getHeight()+1000, 1, 1));
72 // vim:set fo=wancroql tw=80 ts=2 sw=2 sts=2 sta et ai cin ff=unix: