Example usage for javax.swing JEditorPane setEditorKit

List of usage examples for javax.swing JEditorPane setEditorKit

Introduction

In this page you can find the example usage for javax.swing JEditorPane setEditorKit.

Prototype

@BeanProperty(expert = true, description = "the currently installed kit for handling content")
public void setEditorKit(EditorKit kit) 

Source Link

Document

Sets the currently installed kit for handling content.

Usage

From source file:LoadingHTMLDocuments.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Tab Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditorKit(new HTMLEditorKit());

    String filename = "yourFile.htm";

    FileReader reader = new FileReader(filename);
    editorPane.read(reader, filename);/*w w  w.  j a  v a2  s  . c o  m*/

    JScrollPane scrollPane = new JScrollPane(editorPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame fr = new JFrame();
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JEditorPane pane = new JEditorPane();
    pane.setEditorKit(new NewEditorKit());
    pane.setText(/*  w w  w .  j a v  a 2  s .  c  o  m*/
            "test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ");

    StyledDocument doc = (StyledDocument) pane.getDocument();
    MutableAttributeSet attr = new SimpleAttributeSet();
    attr.addAttribute("strike-color", Color.red);
    doc.setCharacterAttributes(0, 9, attr, false);

    attr.addAttribute("strike-color", Color.blue);
    doc.setCharacterAttributes(10, 19, attr, false);
    JScrollPane sp = new JScrollPane(pane);

    fr.getContentPane().add(sp);
    fr.setSize(300, 300);
    fr.setLocationRelativeTo(null);
    fr.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    JEditorPane edPane = new JEditorPane();
    edPane.setContentType("text/html");

    HTMLEditorKit hek = new HTMLEditorKit();

    edPane.setEditorKit(hek);

    HTMLDocument doc = (HTMLDocument) edPane.getDocument();

    doc.insertString(0, "Test testing", null);

    Element[] roots = doc.getRootElements();
    Element body = null;//w ww.  j a v a  2  s.  c om
    for (int i = 0; i < roots[0].getElementCount(); i++) {
        Element element = roots[0].getElement(i);
        if (element.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
            body = element;
            break;
        }
    }

    doc.insertAfterEnd(body, "<img src=" + ClassLoader.getSystemResource("thumbnail.png").toString() + ">");
    frame.add(edPane);

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:LoadSync.java

public static void main(String args[]) {
    final String filename = "Test.html";
    JFrame frame = new JFrame("Loading/Saving Example");
    Container content = frame.getContentPane();

    final JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);//from   w ww .  j  av  a 2  s.  com
    JScrollPane scrollPane = new JScrollPane(editorPane);
    content.add(scrollPane, BorderLayout.CENTER);

    editorPane.setEditorKit(new HTMLEditorKit());

    JPanel panel = new JPanel();

    // Setup actions
    Action loadAction = new AbstractAction() {
        {
            putValue(Action.NAME, "Load");
        }

        public void actionPerformed(ActionEvent e) {
            doLoadCommand(editorPane, filename);
        }
    };
    JButton loadButton = new JButton(loadAction);
    panel.add(loadButton);

    content.add(panel, BorderLayout.SOUTH);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:ReplaceReader.java

public static void main(String[] args) {
    try {//from w  ww .  ja va  2  s  .  c  om
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("JEditorPane with Custom Reader");
    JEditorPane ep = new JEditorPane();
    f.getContentPane().add(new JScrollPane(ep));
    f.setSize(400, 300);
    f.setVisible(true);

    HTMLEditorKit kit = new HTMLEditorKit() {
        public Document createDefaultDocument() {
            HTMLDocument doc = new CustomHTMLDocument(getStyleSheet());
            doc.setAsynchronousLoadPriority(4);
            doc.setTokenThreshold(100);
            return doc;
        }
    };
    ep.setEditorKit(kit);

    try {
        Document doc = ep.getDocument();
        doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
        kit.read(new FileReader(args[0]), doc, 0);
    } catch (Exception e) {
        System.out.println("Exception while reading HTML " + e);
    }
}

From source file:com.net2plan.utils.HTMLUtils.java

/**
 * <p>Saves an HTML content to a given file. Plain text is automatically 
 * wrapped into HTML.</p>/* w  w  w  .  ja v  a2  s.  com*/
 *
 * @param file A valid file
 * @param html HTML content
 */
public static void saveToFile(File file, String html) {
    CustomHTMLEditorKit kit = new CustomHTMLEditorKit();
    JEditorPane editor = new CustomHTMLEditorKit.CustomJEditorPane();
    editor.setEditorKit(kit);
    editor.setContentType("text/html");
    editor.setText(html);
    editor.setText(CustomHTMLEditorKit.includeNet2PlanHeader(editor.getText()));
    CustomHTMLEditorKit.saveToFile(file, editor.getText(), kit.getImages());
}

From source file:Main.java

public Main() throws Exception {
    setSize(400, 240);//from   w w w  . java  2s  .  c o m
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);

    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);

    FileInputStream fi = new FileInputStream("test.rtf");
    rtf.read(fi, editor.getDocument(), 0);
}

From source file:RTFView.java

public RTFView() {
    setTitle("RTF Text Application");
    setSize(400, 240);//from   w ww .jav  a2 s. c  o  m
    setBackground(Color.gray);
    getContentPane().setLayout(new BorderLayout());

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    // Create an RTF editor window
    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);
    editor.setBackground(Color.white);

    // This text could be big so add a scroll pane
    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);

    // Load an RTF file into the editor
    try {
        FileInputStream fi = new FileInputStream("test.rtf");
        rtf.read(fi, editor.getDocument(), 0);
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    } catch (IOException e) {
        System.out.println("I/O error");
    } catch (BadLocationException e) {
    }
}

From source file:Main.java

public Main() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/HTML");
    editorPane.setEditorKit(new HTMLEditorKit());
    editorPane.setText("<hr>Welcome to <b>java2s.com!</b><hr>");
    JToolBar bar = new JToolBar();
    bar.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
    bar.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
    bar.add(new StyledEditorKit.FontSizeAction("12", 12));
    bar.add(new StyledEditorKit.FontSizeAction("14", 14));
    bar.add(new StyledEditorKit.FontSizeAction("16", 16));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.add(bar, BorderLayout.NORTH);
    this.add(editorPane, BorderLayout.CENTER);
    this.pack();//  ww  w .  j a v a  2s  .  co m
    this.setVisible(true);
}

From source file:Main.java

public void createJEditorPane(Container bg, Dimension size) {
    JEditorPane pane = new JEditorPane();
    pane.setEditable(false);/*from  ww  w.  j a  v a  2s  .  co m*/
    HTMLEditorKit editorKit = new HTMLEditorKit();
    pane.setEditorKit(editorKit);
    pane.setSize(size);
    pane.setMinimumSize(size);
    pane.setMaximumSize(size);
    pane.setOpaque(true);
    pane.setText(
            "<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>");
    bg.add(pane, BorderLayout.CENTER);
}