Get style at caret position in a JTextPane

会有一股神秘感。 提交于 2019-12-11 14:00:06

问题


I have a basic text editor that can style documents (bold, underline, italicize, underline, align left/right/center, color, font size/family), and that all works great. The problem I'm having is that I want the style to set my buttons and boxes to the correct styling for where the caret is.

Say, for example, I have the string

the quick brown fox jumps over the lazy dog

When I click between the u and i in quick, I want my italicize button to be toggled on to indicate that the text where the caret is has the italicized styling.

I'm using a JTextPane to hold the text, and a StyledEditorKit for the styling.

Thanks.


回答1:


I think what you are looking for, is

getCharacterAttributes method from the JTextPane

http://download.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#getCharacterAttributes()




回答2:


Ok, just for anyone still looking for and didnt find the answer : for example :

AttributeSet attributeSet = textPane.getCharacterAttributes();
Object bold = attributeSet == null ? null : attributeSet.getAttribute( StyleConstants.Bold );
System.out.println( "Bold : " + bold );


来源:https://stackoverflow.com/questions/4360234/get-style-at-caret-position-in-a-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!