从 JTextPane 中的任何偏移量获取样式
有没有办法获取 Style
、样式名称,或者甚至将 JTextPane
某个位置的 Style
与我给出的样式进行比较插入时的文字?因为出于我的目的,我创建了自定义 JTextPane
、StyledDocument
和 DocumentFilter
。因此,我可以选择 Style
用于常规字母,而另一种样式用于数字。我还设置了切换按钮,切换时将 DocumentFilter
设置为不同的数字格式,而未定期切换数字格式,因此最后您无法仅根据 JTextPane 的 getText 来区分哪些数字受到影响()
方法。因此,唯一的方法是比较我将常规数字样式和特殊数字样式作为常量的样式。我唯一需要想到的是如何为每个角色获取风格。
我知道有 JTextPane's
方法可以从插入符的位置获取 AttributeSet,称为 getCharacterAttributes()
但我认为这对我的问题没有用。
是否需要包含代码示例?我觉得不难想象。如果你想要我,我会把它包括在内。
任何意见将不胜感激。谢谢!
Is there a way to get Style
, a style name or just even compare whether Style
at a certain position of JTextPane
with the style I gave the text when inserting? Because for my purpose I created custom JTextPane
, StyledDocument
and DocumentFilter
. So I could choose Style
to be used for say regular letters and another Style for numbers. I have also toggle button which while toggled sets DocumentFilter
to format numbers differently and while not toggled numbers format regularly so at the end you can't distinguish which numbers have been affected just according to JTextPane's getText()
method. Therefore the only way would be to compare styles which I have both regular and special number style as constants. Only thing I need to come up with is how to get Style for each character.
I know there is JTextPane's
method to get AttributeSet from the caret's position called getCharacterAttributes()
but I think it's no use for my issue.
Is it necessary to include code example? I don't think it's difficult to imagine. If you want me I will include it though.
Any input would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试调用
StyledDocument.getCharacterElement(pos)
获取该位置的字符元素,然后调用Element.getAttributes()
获取其属性集。AttributeSet
包含可以使用样式常量
。Try calling
StyledDocument.getCharacterElement(pos)
to get the character element at that position and then callElement.getAttributes()
to get its attribute set.The
AttributeSet
contains styles which you can retrieve using methods provided byStyleConstants
.