2013-01-08 33 views
3

我在我的應用程序中使用Jtextpane。我需要在jtextpane中添加一些標籤,如 [PKG-MEDIA]。 我希望用戶不要編輯此標籤,而他可以在jtextpane中編輯其他文本。如何讓某些文本在Jtextpane中不能被用戶編輯?

public static void main(String args[]) { 
    JFrame j = new JFrame("Hello!"); 
    j.setSize(200, 200); 
    JTextPane k = new JTextPane(); 
    k.setFont(new Font("Akshar Unicode Regular", Font.PLAIN, 17)); 
    k.setText("this is a test code [PKG-MEDIA]. I want to make this tag [PKG-1234] not editable"); 
    j.add(k); 
    j.setVisible(true); 
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

回答

7

使用DocumentFilter除去周圍的保護範圍的文本。您可以在SERT檢查是否(或刪除)被允許在位置編輯發生

見例如this

5

爲了達到這種效果,你需要實現StyledDocument(儘量延長DefaultStyledDocument)。在那裏,保持文本部分不可編輯的列表,並拒絕改變他們的insertString()remove()

例如,當offset是內部的不可編輯的範圍內一個從insertString()剛剛返回而不做任何更改。

當用戶試圖刪除的文字,只在remove()

相關問題