简体   繁体   English

从JTextArea清除附加的文本

[英]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. 我正在使用JTextArea通过一次将条目追加到区域中来显示ArrayList。 However, when I use the method .setText("") to clear all of the appended entries, they are not removed. 但是,当我使用方法.setText(“”)清除所有附加条目时,它们不会被删除。 Is there anyway to clear the text area? 反正有清除文本区域的方法吗?

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 . 如果您使用的是JTextArea,并且想在输入某些文本后清除所有条目,则只需将该原始的预先添加的文本存储在String中,例如称为myOriginalText Rather than call .setText(""); 而不是调用.setText(""); call .setText(myOriginalText) . 调用.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. 另一个选择是直接使用JTextArea的Document,获取原始文本结尾的索引值,然后删除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. 如果要根据程序的状态添加或删除ArrayList中的每个项目,那么最好不要在JTextArea中显示文本,而在JList中显示文本,从而可以独立处理JList中的每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM