public SOExample() { initComponents(); addText("This is my plain text", null); //Retrieve existing attributes. SimpleAttributeSet previousAttribs = new SimpleAttributeSet (jtp.getInputAttributes() .copyAttributes()); SimpleAttributeSet BOLD = new SimpleAttributeSet(); StyleConstants.setBold (BOLD, true); StyleConstants.setForeground (BOLD, Color.BLUE); //Merge new attributes with existing ones. previousAttribs.addAttributes (BOLD); //Insert the string and apply merged attributes. addText ("This is my BLUE BOLD text", previousAttribs); outputHTMLfile();
public class ExtendedHTMLEditorKit extends HTMLEditorKit{ //.... other code here public class MyHTMLFactory extends HTMLFactory{ //other code here @Override public View create(Element elem) { if (isLayered(elem)){ //it means, it has position attribute return new PositionedView(elem); } else return super.create(elem); } boolean isLayered(Element elem){ SimpleAttributeSet sas = new SimpleAttributeSet(elem); StyleSheet styles = (HTMLDocument elem.getDocument).getStyleSheet(); Tag tag = element.getAttributes().getAttribute(AttributeSet.NameAttribute); sas.addAttributes(styleSheet.getRule(tag, element)); return sas.isDefined("position") && !sas.getAttribute("position").toString().equalsIgnorecase("static"); } } }
private AttributeSet makeAttributes(final int offset, final URI target) { SimpleAttributeSet atts = new SimpleAttributeSet(doc.getCharacterElement(offset).getAttributes()); atts.addAttributes(LINK_ATTRIBUTES); atts.addAttribute(ATTR_LINK, new IAttributeAction() { @Override public void execute() { try { Desktop.getDesktop().browse(target); } catch (Exception e) { JOptionPane.showConfirmDialog(null, e.getLocalizedMessage(), OStrings.getString("ERROR_TITLE"), JOptionPane.ERROR_MESSAGE); Log.log(e); } } }); return atts; } }
as = highlighting.get (ts.offset(), ts.offset() + t.length()); if (as != null) { attributeSet.addAttributes(as); endOffset1 = ts.offset() + t.length(); return;