private void refreshPane() { final int docLength = doc.getLength(); if (docLength == 0) { return; } try { // clear attributes for (int i = 0; i < docLength; ++i) { if (doc.getCharacterElement(i).getAttributes().containsAttributes(LINK_ATTRIBUTES)) { doc.setCharacterAttributes(i, 1, DEFAULT_ATTRIBUTES, true); } } // URL detection final String text = doc.getText(0, docLength); final Matcher matcher = URL_PATTERN.matcher(text); while (matcher.find()) { final int offset = matcher.start(); final int targetLength = matcher.end() - offset; try { // Transform into clickable text AttributeSet atts = makeAttributes(offset, new URI(matcher.group())); doc.setCharacterAttributes(offset, targetLength, atts, true); } catch (URISyntaxException ex) { Log.log(ex); } } } catch (BadLocationException ex) { Log.log(ex); } }
import javax.swing.JTextPane; import javax.swing.text.StyleConstants; import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; public class StyleBugFix { public static void main(String[] args) { JTextPane textPane = new JTextPane(); textPane.setText("This is a test string"); StyleConstants.setBold(BOLD, true); StyleConstants.setItalic(ITALIC, true); int start = 5; int end = 10; textPane.getStyledDocument().setCharacterAttributes(start, end - start, BOLD, false); textPane.getStyledDocument().setCharacterAttributes(start, end - start, ITALIC, false); for(int i = start; i < end; i++) System.out.println(textPane.getStyledDocument().getCharacterElement(i).getAttributes() .containsAttributes(BOLD)); //all now print true } private static final MutableAttributeSet BOLD = new SimpleAttributeSet(); private static final MutableAttributeSet ITALIC = new SimpleAttributeSet(); }
if( tokenElem.getAttributes().containsAttributes( _error ) || tokenElem.getAttributes().containsAttributes( _warning ) )