2 import java
.awt
.Rectangle
;
4 import java
.awt
.Graphics
;
5 import javax
.swing
.JScrollPane
;
6 import javax
.swing
.JTextArea
;
7 import javax
.swing
.JScrollBar
;
11 /** A message area displays text in a scrollable text widget. It is a
12 singleton. Other objects can access it directly to display messages.
14 public class MessageArea
17 public static synchronized MessageArea
Instance ()
19 if (saInstance
== null)
20 saInstance
= new MessageArea ();
27 /** Create a new message area. This method is private because the class is
28 a singleton and may therefore not be instanciated from the outside.
30 private MessageArea ()
32 maText
= new JTextArea();
33 maText
.setBackground (new Color (255,250,240));
34 maText
.setFont (new Font ("Helvetica", Font
.PLAIN
, 9));
35 setViewportView (maText
);
36 setVerticalScrollBarPolicy (JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
);
37 setHorizontalScrollBarPolicy (JScrollPane
.HORIZONTAL_SCROLLBAR_ALWAYS
);
40 "class path is " + System
.getProperty ("java.class.path") + "\n");
46 /** Show the given string at the end of the message area and scroll to make
49 public static synchronized void print (String aMessage
)
57 /** Show the given string at the end of the message area and scroll to make
58 it visible. Indent the string as requested.
60 public static synchronized void print (int nIndentation
, String aMessage
)
62 while (nIndentation
-- > 0)
63 aMessage
= " " + aMessage
;
64 Instance().printMessage(aMessage
);
70 /** Show the given string at the end of the message area and scroll to make
73 public static void println (String aMessage
)
75 println (0, aMessage
);
81 /** Show the given string at the end of the message area and scroll to make
84 public static void println (int nIndentation
, String aMessage
)
86 print (nIndentation
, aMessage
+"\n");
92 public void paintComponent (Graphics g
)
96 JScrollBar sb
= getVerticalScrollBar();
99 int nScrollBarValue
= sb
.getMaximum() - sb
.getVisibleAmount() - 1;
100 sb
.setValue (nScrollBarValue
);
102 super.paintComponent (g
);
109 /** Append the given string to the end of the text and scroll so that it
110 becomes visible. This is an internal method. Use one of the static
113 private synchronized void printMessage (String aMessage
)
115 maText
.append (aMessage
);
121 private static MessageArea saInstance
= null;
122 private JTextArea maText
;