The moose likes Swing / AWT / SWT / JFace and the fly likes JTextPane not reflecting changes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JTextPane not reflecting changes" Watch "JTextPane not reflecting changes" New topic
Author

JTextPane not reflecting changes

selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
Hi folks,
Here is the code snippet which gives me a problem..
On trying this code below i am able to add bold to the selected
portion of the text, but when i try to remove the bold portion in the textpane it is not getting removed from UI??What may be
the problem??
contentTextPane is the reference for the textpane.
set -- contentTextPane .setEditorKit(new HTMLEditorKit()) ;
you can put the following in an actionlistener for a button.
Try selecting a portion in the textpane and click the button ....
int startpostion = 0;
int mark = contentTextPane.getCaret().getMark();
int dot = contentTextPane.getCaret().getDot();
int end = 0;
if(mark<dot){>
startpostion =mark;
end = dot;
}else {
startpostion = dot;
end =mark;
}

Element element = contentTextPane.getStyledDocument().getCharacterElement(startpostion);
AttributeSet attrs = element.getAttributes();
SimpleAttributeSet simpleAttr = new SimpleAttributeSet();
simpleAttr.addAttributes(attrs);
if(StyleConstants.isBold(attrs)){
StyleConstants.setBold(simpleAttr,false);
simpleAttr.removeAttribute(HTML.Tag.B);
}else
StyleConstants.setBold(simpleAttr,true);

contentTextPane.getStyledDocument().setCharacterAttributes(startpostion,end-startpostion, simpleAttr, true);
 
 
subject: JTextPane not reflecting changes
 
Threads others viewed
Alignment in JTextArea/JTextPane
Find index by xy ??
Color specifics words in a JTextPane
run this file
JTextPane --find the mystery
developer file tools