简体   繁体   中英

How to identify if the selected text in JTextPane are of different sizes?

I am developing a editor where I have a combobox for font size. User can use it to alter the text size.

I have also implemented the caret listener which tells me the size of selected text and updates the combobox accordingly. Now if the user selects the text of two different sizes I want to populate blank value in Combobox. But I am unable to do that with Caret listener as It gives me size of the first text.

Eg: If my text is "HI". Here H is of size 12 and I is of size 22. Now when I select "HI" caret listener gives me the value of either 12 or 22.

Here is the sample code:

StyledDocument doc = pane.getStyledDocument();
MutableAttributeSet fontSizeStyle = ((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
int fontSize = StyleConstants.getFontSize(fontSizeStyle);

Below code would loop through the selected text in Jtextpane and get each character's size.

String selectedText = pane.getSelectedText();
int k = pane.getSelectionStart();

for(int i=0; i< selectedText.length(); i++) {
   AttributeSet fontSize = doc.getCharacterElement(k).getAttributes();                       
   System.out.println("fontSize:"+StyleConstants.getFontSize(fontSize));
   k++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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