如何在Jtextpane中使用户无法编辑某些文本?

May*_*ngh 2 java swing jtextpane

我在我的应用程序中使用Jtextpane.我需要在jtextpane中添加一些像[PKG-MEDIA]的标签.我希望用户不要编辑这个标签,而他可以在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);
}
Run Code Online (Sandbox Code Playgroud)

Sta*_*avL 7

使用DocumentFilter.您可以检查在位置编辑中是否允许使用sert(或remove)

例如,见