// Updates: 2004.09.30 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * An improvement of the JTextArea, containing scrollbars that are automatically * made up-to-date when new text is added at the end of the text zone. * @author Michel Deriaz */ public class TextZone extends JPanel { private JTextArea ta; /** * Creates a text zone with no empty border around it and no title. */ public TextZone() { this(null, 0); } /** * Creates a text zone with an empty border around it and a title. * @param title the title of the text zone * @param border the size (in pixels) of the empty border around the text zone */ public TextZone(String title, int border) { setLayout(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder(border, border, border, border)); JPanel p = new JPanel(); p.setLayout(new BorderLayout()); if (title != null) p.setBorder(BorderFactory.createTitledBorder(" " + title + " ")); ta = new JTextArea(); ta.setForeground(Color.BLUE); ta.setEditable(false); ta.setMargin(new Insets(5, 5, 5, 5)); p.add(new JScrollPane(ta), BorderLayout.CENTER); add(p, BorderLayout.CENTER); } /** * Adds the specfied text to the end of the text zone. * @param text the text to add */ public void print(String text) { ta.append(text); try { ta.scrollRectToVisible(ta.modelToView(ta.getDocument().getLength())); } catch (javax.swing.text.BadLocationException e) { System.out.println(e); } } /** * Adds the specfied text to the end of the text zone and terminates the line. * @param text the text to add */ public void println(String text) { print(text + "\n"); } /** * Gets the current text. * @return the text */ public String getText() { return ta.getText(); } /** * Sets the current text. * @param text the text */ public void setText(String text) { ta.setText(text); } }