Example usage for javax.swing JTextPane setDocument

List of usage examples for javax.swing JTextPane setDocument

Introduction

In this page you can find the example usage for javax.swing JTextPane setDocument.

Prototype

public void setDocument(Document doc) 

Source Link

Document

Associates the editor with a text document.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane c1 = new JTextPane();
    JTextPane c2 = new JTextPane();

    StyleContext styleContext = new StyleContext();
    c1.setDocument(new DefaultStyledDocument(styleContext));
    c2.setDocument(new DefaultStyledDocument(styleContext));

    Style style = c1.addStyle("style name", null);
    StyleConstants.setForeground(style, Color.red);

    style = c2.getStyle("style name");
    StyleConstants.setBold(style, true);
}

From source file:Main.java

public static void main(String[] args) {
    String HTMLTEXT = "<html><head><style>.foot{color:red} .head{color:black}</style></head>"
            + "<span style='font-family:consolas'>java2s.com</span><br/>"
            + "<span style='font-family:tahoma'>java2s.com</span>";
    JTextPane textPane1 = new JTextPane();

    textPane1.setContentType("text/html");
    textPane1.setFont(new Font("courier new", Font.PLAIN, 32));
    textPane1.setDocument(new HTMLDocument() {
        @Override//from  w  w  w  . j av a  2 s . c  o  m
        public Font getFont(AttributeSet attr) {
            StyleContext styles = (StyleContext) getAttributeContext();
            Font f = styles.getFont(attr);
            String ff = f.getFamily();
            System.out.println(ff);
            return textPane1.getFont();
        }
    });
    textPane1.setText(HTMLTEXT);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:com.hp.alm.ali.idea.ui.editor.field.HTMLAreaField.java

public static void enableCapability(final JTextPane desc, Project project, String value, boolean editable,
        boolean navigation) {
    value = removeSmallFont(value);/*from  w ww. j a  v  a  2s.c  om*/
    HTMLEditorKit kit = new HTMLLetterWrappingEditorKit();
    desc.setEditorKit(kit);
    desc.setDocument(kit.createDefaultDocument());
    if (!editable && navigation) {
        value = NavigationDecorator.explodeHtml(project, value);
    }
    desc.setText(value);
    if (!editable) {
        desc.setCaret(new NonAdjustingCaret());
    }
    desc.addCaretListener(new BodyLimitCaretListener(desc));
    if (editable) {
        String element = checkElements(desc.getDocument().getDefaultRootElement());
        if (element != null) {
            desc.setToolTipText("Found unsupported element '" + element + "', editing is disabled.");
            editable = false;
        }
    }
    desc.setEditable(editable);

    if (editable && SpellCheckerManager.isAvailable()
            && ApplicationManager.getApplication().getComponent(AliConfiguration.class).spellChecker) {
        desc.getDocument().addDocumentListener(new SpellCheckDocumentListener(project, desc));
    }

    Font font = UIManager.getFont("Label.font");
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize()
            + "pt; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(bodyRule);

    // AGM uses plain "p" to create lines, we need to avoid excessive spacing this by default creates
    String paragraphRule = "p { margin-top: 0px; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(paragraphRule);

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // following is needed to make copy work in the IDE
            try {
                StringSelection selection = new StringSelection(desc.getText(desc.getSelectionStart(),
                        desc.getSelectionEnd() - desc.getSelectionStart()));
                CopyPasteManager.getInstance().setContents(selection);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_COPY)), desc);
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // avoid pasting non-supported HTML markup by always converting to plain text
            Transferable contents = CopyPasteManager.getInstance().getContents();
            try {
                desc.getActionMap().get(DefaultEditorKit.cutAction).actionPerformed(null);
                desc.getDocument().insertString(desc.getSelectionStart(),
                        (String) contents.getTransferData(DataFlavor.stringFlavor), null);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_PASTE)), desc);
    installNavigationShortCuts(desc);

}

From source file:Main.java

public Main() {
    setSize(400, 400);//from  w ww .ja v  a2s  . c  o m
    styleSheet.addRule(".someclass1 {color: blue;}");
    styleSheet.addRule(".someclass2 {color: green;}");

    htmlEditorKit.setStyleSheet(styleSheet);
    htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument();
    JTextPane jTextPane = new JTextPane();
    jTextPane.setEditorKit(htmlEditorKit);
    jTextPane.setDocument(htmlDocument);

    try {
        Element htmlElement = htmlDocument.getRootElements()[0];
        bodyElement = htmlElement.getElement(0);

        Container contentPane = getContentPane();
        contentPane.add(jTextPane, BorderLayout.CENTER);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addContent("<span class=someclass1>test 1</span><br>");
        addContent("<span class=someclass2>test 2</span><br>");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:es.ubu.XRayDetector.interfaz.PanelAplicacion.java

/**
 * Gets the application log.//from w  ww. j  a v  a2 s.  c o  m
 * 
 * @param panelLog The panel of the log.
 * @return The application log.
 */
private JTextPane getTxtLog(JPanel panelLog) {
    JTextPane textPaneLog = new JTextPane();
    textPaneLog.setBounds(4, 4, 209, 195);
    textPaneLog.setEditable(false);
    panelLog.add(textPaneLog);
    textPaneLog.setContentType("text/html");
    kit = new HTMLEditorKit();
    doc = new HTMLDocument();
    panelLog_1.setLayout(null);
    textPaneLog.setEditorKit(kit);
    textPaneLog.setDocument(doc);

    textPaneLog.setText("<!DOCTYPE html>" + "<html>" + "<head>" + "<style>" + "p.normal {font-weight:normal;}"
            + "p.error {font-weight:bold; color:red}" + "p.exito {font-weight:bold; color:green}"
            + "p.stop {font-weight:bold; color:blue}" + "</style>" + "</head>" + "<body>");
    JScrollPane scroll = new JScrollPane(textPaneLog);
    scroll.setBounds(10, 16, 242, 254);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    panelLog.add(scroll);
    return textPaneLog;
}