Clear a JTextArea
807569Jun 15 2006 — edited Jun 15 2006I had the same problem other had discussed in this forum.
I want to clear the tex my text area contained after I type carriage return.
I tried with myTextArea.setText("") or myTextArea.setText(null) but thy caused an Exception: IllegalStateException.I put my simple code as you can examine it.
Could help me?
Here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.event.DocumentEvent;
import javax.swing.text.Document;
import javax.swing.event.DocumentListener;
public class TextDemo extends JPanel {
protected JTextArea textField;
protected JTextArea textArea;
private final static String newline = "\n";
public TextDemo() {
super(new GridBagLayout());
textField = new JTextArea(5,20);
textField.getDocument().addDocumentListener(new MyDocumentListener());
textField.setEditable(true);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
class MyDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
String text = textField.getText();
if(text.endsWith(newline))
updateLog(e);
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
}
public void updateLog(DocumentEvent e){
String text = textField.getText();
textField.removeAll();
//textField.setText("");
textArea.append(text + newline);
textField.selectAll();
//Make sure the new text is visible, even if there
//was a selection in the text area.
//textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new TextDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}