简体   繁体   English

JTextPane DefaultStyledDocument和setEditorKit冲突

[英]JTextPane DefaultStyledDocument and setEditorKit conflict

I'm using JTextPane in a project where I'm supposed to display row numbers and color some keywords. 我在一个项目中使用JTextPane,该项目应该显示行号并为一些关键字加上颜色。 For displaying row numbers, I'm using 'setEditorKit' and for coloring the text, I'm overriding insertString() and removeString() methods of DefaultStyledDocument and then using 'JTextPane.setDocument(DefaultStyledDocument_object)'. 为了显示行号,我使用了“ setEditorKit”,并且为文本着色,我覆盖了DefaultStyledDocument的insertString()和removeString()方法,然后使用了“ JTextPane.setDocument(DefaultStyledDocument_object)”。

The problem I'm facing is that if I add EditorKit to my JTextPane, the coloring part doesn't work. 我面临的问题是,如果将EditorKit添加到我的JTextPane中,则着色部分无法正常工作。 And if I add the the DefaultStyledDocument after setting the EditorKit, the insertString method shifts the caret to the 'zero caret position', hence messing up the UI. 而且,如果我在设置EditorKit后添加DefaultStyledDocument,则insertString方法会将插入符号移动到“零插入符号位置”,从而使UI混乱。

Here's an image of the functionality I'm trying to achive. 这是我要实现的功能的图像。

我想要的是
This is what I want to my JTextPane to look like. 这就是我想要的JTextPane外观。

混乱
This is what it currently looks like 这是目前的样子

How do I use both 'setEditorKit' and 'DefaultStyledDocument' in my application? 如何在应用程序中同时使用“ setEditorKit”和“ DefaultStyledDocument”?

Don't use row numbers as part of the Document. 不要将行号用作文档的一部分。 Instead you should create a component that displays row numbers and use the component as a row header for your scroll pane. 相反,您应该创建一个显示行号的组件,并将该组件用作滚动窗格的行标题。

See Text Component Line Number for an example of this approach. 有关此方法的示例,请参见文本组件行号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM