2017-08-17 94 views
0

我正在使用JTextArea通過將條目追加到一次一個區域來顯示ArrayList。但是,當我使用方法.setText(「」)清除所有附加條目時,它們不會被刪除。無論如何要清除文本區域?從JTextArea清除附加文本

+0

如果你正在做的事情,然後'setText(「」)'應該工作。你確定你沒有在主線程中做所有事情,所以GUI從來沒有機會更新? – John3136

+0

setText(「」)'在按下按鈕後運行,所以它不在主線程中 – Jeremy

+0

如果你在ActionHandler中執行它,它實際上是在gui線程中 – vatbub

回答

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]與您的問題(例如,請參閱上面我的代碼),以便我們可以更好地解決問題。 –