我正在使用JTextArea通過將條目追加到一次一個區域來顯示ArrayList。但是,當我使用方法.setText(「」)清除所有附加條目時,它們不會被刪除。無論如何要清除文本區域?從JTextArea清除附加文本
0
A
回答
2
如果您使用的是JTextArea,並且您希望在輸入某些文本後清除所有條目,那麼只需將該預先附加的原始文本存儲在字符串中,稱爲myOriginalText
。不要致電 .setText("");
致電.setText(myOriginalText)
。另一個選擇是直接使用JTextArea的Document,獲取原始文本末尾的索引值,然後刪除Document中該索引之後的所有文本。
例如:
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());
}
}
如果在ArrayList中的每個項目是根據程序的狀態下進行添加或刪除,那麼也許你可能最好不要顯示一個JTextArea中的文本,但而是一個JList,您可以獨立處理JList中的每個項目。
+0
謝謝。我發現我追加的列表實際上是問題,而不是.setText – Jeremy
+0
@Jeremy:將來,發佈一個有效的[mcve]與您的問題(例如,請參閱上面我的代碼),以便我們可以更好地解決問題。 –
相關問題
- 1. 在Java的JTextArea中附加文本
- 2. JTextArea附加
- 3. JTextArea附加問題
- 4. 清除第一行jtextarea
- 5. Jquery清除附加內容
- 6. 在使用DocumentFilter後將文本附加到JTextArea
- 7. 從JSpinner到JTextArea的文本
- 8. 將文本文件加載到JtextArea
- 9. 反應本機文本輸入清除不清除文本
- 10. 清除文本框
- 11. jQuery的移除附加文本
- 12. 清除加載的所有文本框
- 13. 不能附加字符串到jtextarea
- 14. 附加屬性清除本地值但xaml不刷新
- 15. 從textarea清除硬編碼文本
- 16. 從python shell中清除文本
- 17. 從textarea用硒清除文本
- 18. 如何從JTextArea獲取文本時刪除斷裂線
- 19. 添加多行文本到JTextArea中
- 20. 如何從JTextArea中獲取文本?
- 21. 附加文本Javascript
- 22. 刪除/清除打開文本文件
- 23. 附加到Jtextarea顯示文件位置不是掃描內容
- 24. 文本框asp.net的清除文本值?
- 25. 清除標籤文本值
- 26. 清除文本按鈕
- 27. 清除輸入文本AngularJS
- 28. 清除編輯文本 - adb
- 29. 如何清除文本框?
- 30. 附加到文本文件
如果你正在做的事情,然後'setText(「」)'應該工作。你確定你沒有在主線程中做所有事情,所以GUI從來沒有機會更新? – John3136
setText(「」)'在按下按鈕後運行,所以它不在主線程中 – Jeremy
如果你在ActionHandler中執行它,它實際上是在gui線程中 – vatbub