我在我的应用程序中使用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);
}
最佳答案
使用DocumentFilter
。您可以检查在进行位置编辑时是否允许插入(或删除)
例如参见this