2015-06-05 45 views
0

我想創建一個JTextArea,用戶無法擦除上一行。就像Windows中的命令提示符和Linux中的終端一樣,您不能編輯以前的行。限制對JTextArea中某些行的訪問?

這就是我想到的,但它似乎並沒有工作,我只能想出一個原因,但它似乎不僅僅是一個原因。

if(commandArea.getCaretPosition() < commandArea.getText().lastIndexOf("\n")){ 
     commandArea.setCaretPosition(commandArea.getText().lastIndexOf("\n")); 
} 

的代碼塊住這個方法裏面:

private void commandAreaKeyPressed(java.awt.event.KeyEvent evt) 
+1

我建議容易的解決方案 - 命令行歷史可不可編輯的JTextArea,而當前命令可編輯的JTextArea/JTextField中。在我的項目中工作得很好 - 具有歷史的高級計算器。也就是說,除非你需要一行可編輯的下一個否,第三是的等。 – peenut

+0

謝謝你的回答@peenut。我沒有使用JTextField輸入命令和JTextArea來顯示輸出,但我想改變它,但仍然感謝你! – Darawan

回答

2

可以使用的DocumentFilter爲JTextArea中的文件。可運行的工作示例,允許僅在JTextArea中編輯最後一行:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.DocumentFilter.FilterBypass; 

public class MainClass { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("text area test"); 
     JPanel panelContent = new JPanel(new BorderLayout()); 
     frame.setContentPane(panelContent); 
     UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font")); //let text area respect DPI 
     panelContent.add(createSpecialTextArea(), BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(800, 600); 
     frame.setLocationRelativeTo(null); //center screen 
     frame.setVisible(true); 
    } 

    private static JTextArea createSpecialTextArea() { 
     final JTextArea textArea = new JTextArea("first line\nsecond line\nthird line"); 
     ((AbstractDocument)textArea.getDocument()).setDocumentFilter(new DocumentFilter() { 

      private boolean allowChange(int offset) { 
       try { 
       int offsetLastLine = textArea.getLineCount() == 0 ? 0 : textArea.getLineStartOffset(textArea.getLineCount() - 1); 
       return offset >= offsetLastLine; 
       } catch (BadLocationException ex) { 
        throw new RuntimeException(ex); //should never happen anyway 
       } 
      } 

      @Override 
      public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { 
       if (allowChange(offset)) { 
        super.remove(fb, offset, length); 
       } 
      } 

      @Override 
      public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { 
       if (allowChange(offset)) { 
        super.replace(fb, offset, length, text, attrs); 
       } 
      } 

      @Override 
      public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { 
       if (allowChange(offset)) { 
        super.insertString(fb, offset, string, attr); 
       } 
      } 



     }); 
     return textArea; 
    } 
} 

它是如何工作的? JTextArea是一個文本控件,實際數據有Document。 Document允許監聽更改(DocumentListener),並且某些文檔允許將DocumentFilter設置爲禁止更改。 PlainDocument和DefaultStyledDocument均從AbstractDocument擴展,允許設置過濾器。

請務必仔細閱讀Java文檔:

我還推薦了一些教程:

+0

你從哪裏學到的?你能否將我鏈接到本書或在線教程,如果它在那裏... – Darawan

+0

添加了在線官方Java文檔的鏈接,解釋我所瞭解並用於解決問題的內容。 – peenut

+0

(1+)爲'DocumentFilter',您也可以查看[受保護的文本組件](https://tips4java.wordpress.com/2008/12/21/protected-text-component/)以獲得允許的解決方案文檔的多個區域需要單獨保護。 – camickr

相關問題