Jeremy Jeremy - 1 year ago 142
Java Question

Clear Appended Text from JTextArea

I am using a JTextArea to display an ArrayList by appending the entries to the area one at a time. However, when I use the method .setText("") to clear all of the appended entries, they are not removed. Is there anyway to clear the text area?

Answer Source

If you're using a JTextArea, and you want to clear all entries after certain text has been entered, then simply store that originl pre-appended text in a String, say called myOriginalText. Rather than call .setText(""); call .setText(myOriginalText). Another option is to work directly with the JTextArea's Document, getting the index value of the end of the original text, and then removing all text after that index within the Document.

For example:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class TextAreaFun extends JPanel {
    private JTextArea textArea = new JTextArea(20, 40);
    private JTextField textEntry = new JTextField(25);
    private AppendAction appendAction = new AppendAction("Append Text");
    private ProtectAction protectAction = new ProtectAction("Protect Text");
    private ClearAction clearAction = new ClearAction("Clear Text");
    private String protectedText = "";

    public TextAreaFun() {
        textArea.setFocusable(false);
        textArea.setEditable(false);
        JScrollPane taScrollPane = new JScrollPane(textArea);
        taScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        textEntry.setAction(appendAction);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
        bottomPanel.add(textEntry);
        bottomPanel.add(new JButton(appendAction));
        bottomPanel.add(new JButton(protectAction));
        bottomPanel.add(new JButton(clearAction));        

        setLayout(new BorderLayout());
        add(taScrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    private class AppendAction extends AbstractAction {
        public AppendAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Append text to text area");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.append(textEntry.getText() + "\n");
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private class ProtectAction extends AbstractAction {
        public ProtectAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Protext text in text area from being cleared");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            protectedText = textArea.getText();
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private class ClearAction extends AbstractAction {
        public ClearAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Clear unprotected text from text area");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.setText(protectedText);
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private static void createAndShowGui() {
        TextAreaFun mainPanel = new TextAreaFun();

        JFrame frame = new JFrame("Text Area Fun");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

If each item within the ArrayList is to be added or removed depending on the state of the program, then perhaps you're likely better off not displaying the text within a JTextArea but rather a JList where you can handle each item within the JList independently.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download