• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane not reflecting changes

 
selvas kumars
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!