Example usage for javax.swing JEditorPane setText

List of usage examples for javax.swing JEditorPane setText

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the text of this component")
public void setText(String t) 

Source Link

Document

Sets the text of this TextComponent to the specified content, which is expected to be in the format of the content type of this editor.

Usage

From source file:CheckThreadViolationRepaintManager.java

static void imageUpdateTest() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JEditorPane editor = new JEditorPane();
    frame.setContentPane(editor);//from  w  w w  . j a v  a2  s.c  om
    editor.setContentType("text/html");
    // it works with no valid image as well
    editor.setText("<html><img src=\"file:\\lala.png\"></html>");
    frame.setSize(300, 200);
    frame.setVisible(true);
}

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>//from  www. j  a va2s  . co m
 *
 * @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

private void addComponents() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/html");
    editorPane.setEditable(false);/*ww w  . jav  a  2s. c o m*/
    editorPane.setText(TEXT);

    JScrollPane scrollpane = new JScrollPane(editorPane);

    editorPane.addHyperlinkListener(e -> {
        if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
            String description = e.getDescription();
            if (TOP.equals(description)) {
                JViewport viewport = scrollpane.getViewport();
                viewport.setViewPosition(new Point(0, 0));
            }
        }
    });
    super.add(scrollpane);
}

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();//w  ww  .ja va  2  s  .c  o  m
    this.setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationByPlatform(true);/*from  w  w w  .  j  a  v a 2  s .  c o m*/

    JEditorPane editPane = new JEditorPane();
    JScrollPane scrollPane = new JScrollPane(editPane);

    editPane.setContentType("text/html");

    editPane.setText("<html><p style = \"text-align:center;\">Hello there, How you doing ?<img src = "
            + "\"http://www.java2s.com/style/download.png"
            + "\" alt = \"pic\" width = \"15\" height = \"15\" />test test test"
            + "<br />I hope this is what you wanted!! "
            + "<img src =  \"http://www.java2s.com/style/download.png"
            + "\" alt = \"pic\" width = \"15\" height = \"15\" /> this is a test.</p></html>\n");

    add(scrollPane, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
}

From source file:Main.java

public Main() {

    JEditorPane web = new JEditorPane();
    web.setEditable(false);//from w  ww  .  j  a  v  a2  s  .  co m

    try {
        web.setPage("http://www.cnn.com");
    } catch (IOException e) {
        web.setContentType("text/html");
        web.setText("<html>Could not load</html>");
    }

    final JScrollPane scrollPane = new JScrollPane(web);
    getContentPane().add(scrollPane);
    this.setBounds(0, 0, 200, 200);

}

From source file:Main.java

public void createJEditorPane(Container bg, Dimension size) {
    JEditorPane pane = new JEditorPane();
    pane.setEditable(false);// w ww . j  a v  a 2 s. com
    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);
}

From source file:Main.java

public Main() {
    JEditorPane editorPane = new JEditorPane();
    JScrollPane scrollPane = new JScrollPane();

    this.setPreferredSize(new Dimension(300, 300));
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    editorPane.setEditorKit(new PreWrapHTMLEditorKit());
    editorPane.setText("" + "<html>" + "<head></head>" + "<body>"
            + "<pre>long text line long text line long text line long text line (two new lines here!)\n\n"
            + "long text line long text line long text line long text line long text line long text line</pre>"
            + "</body>" + "</html>");
    scrollPane.setViewportView(editorPane);
    getContentPane().add(scrollPane);//  w  w  w .  ja va 2  s  .  co m
    pack();
}

From source file:com.mirth.connect.plugins.rtfviewer.RTFViewer.java

@Override
public void viewAttachments(List<String> attachmentIds) {
    // do viewing code

    Frame frame = new Frame("RTF Viewer");

    frame.setLayout(new BorderLayout());

    try {//from   www.  j  a v  a 2s  .  c o m

        Attachment attachment = parent.mirthClient.getAttachment(attachmentIds.get(0));
        byte[] rawRTF = Base64.decodeBase64(attachment.getData());
        JEditorPane jEditorPane = new JEditorPane("text/rtf", new String(rawRTF));

        if (jEditorPane.getDocument().getLength() == 0) {
            // decoded when it should not have been.  i.e.) the attachment data was not encoded.
            jEditorPane.setText(new String(attachment.getData()));
        }

        jEditorPane.setEditable(false);
        JScrollPane scrollPane = new javax.swing.JScrollPane();
        scrollPane.setViewportView(jEditorPane);
        frame.add(scrollPane);
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                e.getWindow().dispose();
            }
        });

        frame.setSize(600, 800);

        Dimension dlgSize = frame.getSize();
        Dimension frmSize = parent.getSize();
        Point loc = parent.getLocation();

        if ((frmSize.width == 0 && frmSize.height == 0) || (loc.x == 0 && loc.y == 0)) {
            frame.setLocationRelativeTo(null);
        } else {
            frame.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                    (frmSize.height - dlgSize.height) / 2 + loc.y);
        }

        frame.setVisible(true);
    } catch (Exception e) {
        parent.alertException(parent, e.getStackTrace(), e.getMessage());
    }
}

From source file:com.mirth.connect.plugins.textviewer.TextViewer.java

@Override
public void viewAttachments(String channelId, Long messageId, String attachmentId) {
    // do viewing code
    Frame frame = new Frame("Text Viewer");
    frame.setLayout(new BorderLayout());

    try {/*w  ww .  j  ava  2s . c  o m*/
        Attachment attachment = parent.mirthClient.getAttachment(channelId, messageId, attachmentId);
        byte[] content = Base64.decodeBase64(attachment.getContent());

        boolean isRTF = attachment.getType().toLowerCase().contains("rtf");
        //TODO set character encoding
        JEditorPane jEditorPane = new JEditorPane(isRTF ? "text/rtf" : "text/plain", new String(content));

        if (jEditorPane.getDocument().getLength() == 0) {
            // decoded when it should not have been.  i.e.) the attachment data was not encoded.
            jEditorPane.setText(new String(attachment.getContent()));
        }

        jEditorPane.setEditable(false);
        JScrollPane scrollPane = new javax.swing.JScrollPane();
        scrollPane.setViewportView(jEditorPane);
        frame.add(scrollPane);
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                e.getWindow().dispose();
            }
        });

        frame.setSize(600, 800);

        Dimension dlgSize = frame.getSize();
        Dimension frmSize = parent.getSize();
        Point loc = parent.getLocation();

        if ((frmSize.width == 0 && frmSize.height == 0) || (loc.x == 0 && loc.y == 0)) {
            frame.setLocationRelativeTo(null);
        } else {
            frame.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                    (frmSize.height - dlgSize.height) / 2 + loc.y);
        }

        frame.setVisible(true);
    } catch (Exception e) {
        parent.alertThrowable(parent, e);
    }
}