Example usage for javax.swing JEditorPane setContentType

List of usage examples for javax.swing JEditorPane setContentType

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the type of content")
public final void setContentType(String type) 

Source Link

Document

Sets the type of content that this editor handles.

Usage

From source file:Main.java

private void addComponents() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/html");
    editorPane.setEditable(false);/*from w  ww .  jav  a  2  s. 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() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationByPlatform(true);//from ww  w  .jav a  2s.  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);/* ww w .j  av  a 2 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:embedding.ExampleJava2D2PDF.java

/**
 * Creates a PDF file. The contents are painted using a Graphics2D implementation that
 * generates an PDF file.//from   w  w  w.  j av  a 2  s  . c  o m
 * @param outputFile the target file
 * @throws IOException In case of an I/O error
 * @throws ConfigurationException if an error occurs configuring the PDF output
 */
public void generatePDF(File outputFile) throws IOException, ConfigurationException {
    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    try {

        //Instantiate the PDFDocumentGraphics2D instance
        PDFDocumentGraphics2D g2d = new PDFDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

        //Configure the G2D with the necessary fonts
        configure(g2d, createAutoFontsConfiguration());

        //Set up the document size
        Dimension pageSize = new Dimension((int) Math.ceil(UnitConv.mm2pt(210)),
                (int) Math.ceil(UnitConv.mm2pt(297))); //page size A4 (in pt)
        g2d.setupDocument(out, pageSize.width, pageSize.height);
        g2d.translate(144, 72); //Establish some page borders

        //A few rectangles rotated and with different color
        Graphics2D copy = (Graphics2D) g2d.create();
        int c = 12;
        for (int i = 0; i < c; i++) {
            float f = ((i + 1) / (float) c);
            Color col = new Color(0.0f, 1 - f, 0.0f);
            copy.setColor(col);
            copy.fillRect(70, 90, 50, 50);
            copy.rotate(-2 * Math.PI / c, 70, 90);
        }
        copy.dispose();

        //Some text
        g2d.rotate(-0.25);
        g2d.setColor(Color.RED);
        g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 140);
        g2d.setColor(Color.RED.darker());
        g2d.setFont(new Font("serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 180);

        pageSize = new Dimension(pageSize.height, pageSize.width);
        g2d.nextPage(pageSize.width, pageSize.height);

        //Demonstrate painting rich text
        String someHTML = "<html><body style=\"font-family:Verdana\">" + "<p>Welcome to <b>page 2!</b></p>"
                + "<h2>PDFDocumentGraphics2D Demonstration</h2>"
                + "<p>We can <i>easily</i> paint some HTML here!</p>" + "<p style=\"color:green;\">"
                + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin accumsan"
                + " condimentum ullamcorper. Sed varius quam id arcu fermentum luctus. Praesent"
                + " nisi ligula, cursus sed vestibulum vel, sodales sed lectus.</p>" + "</body></html>";
        JEditorPane htmlComp = new JEditorPane();
        htmlComp.setContentType("text/html");
        htmlComp.read(new StringReader(someHTML), null);
        htmlComp.setSize(new Dimension(pageSize.width - 72, pageSize.height - 72));
        //htmlComp.setBackground(Color.ORANGE);
        htmlComp.validate();
        htmlComp.printAll(g2d);

        //Cleanup
        g2d.finish();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:io.github.jeddict.jpa.modeler.properties.classmember.ClassMemberPanel.java

private String getCode(String code, String title) {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/x-java");
    editorPane.setPreferredSize(new java.awt.Dimension(600, 400));
    editorPane.setText(code);/*from  w  ww .  j ava  2  s.  co  m*/
    OptionDialog dialog = new OptionDialog(editorPane, title);
    dialog.setVisible(true);
    if (OK_OPTION == dialog.getDialogResult()) {
        return editorPane.getText();
    } else {
        return code;
    }
}

From source file:eu.apenet.dpt.standalone.gui.APETabbedPane.java

public JComponent createMsgEditionTree(String msg) {
    JEditorPane waitMessagePane = new JEditorPane();
    waitMessagePane.setEditable(false);/*from  w w w.j  a va2  s .co  m*/
    waitMessagePane.setContentType("text/plain");
    waitMessagePane.setText(msg);
    waitMessagePane.setCaretPosition(0);
    return waitMessagePane;
}

From source file:com.enderville.enderinstaller.ui.Installer.java

private void loadModDescription(String modName) {
    JPanel p = getModDescriptionPane();
    p.removeAll();/*from w ww. ja  va 2  s  .c  om*/
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

    final String extras = InstallerConfig.getExtraModsFolder();
    final String modFolderName = FilenameUtils.concat(extras, modName);
    File modFolder = new File(modFolderName);
    if (!modFolder.exists()) {
        LOGGER.error("Mod folder for " + modName + " does not exist.");
    }
    File descrFile = new File(FilenameUtils.concat(modFolderName, "description.txt"));
    File imgFile = new File(FilenameUtils.concat(modFolderName, "image.png"));
    if (!descrFile.exists() && !imgFile.exists()) {
        p.add(new JLabel("<html>No description for:<br>" + modName + "</html>"));
    } else {
        if (imgFile.exists()) {
            try {
                JLabel label = new JLabel();
                BufferedImage img = ImageIO.read(imgFile);
                label.setIcon(new ImageIcon(img));
                p.add(label);
            } catch (IOException e) {
                LOGGER.error("Error reading image file: " + imgFile.getPath(), e);
            }
        }
        if (descrFile.exists()) {
            StringBuilder buffer = new StringBuilder();
            try {
                BufferedReader r = new BufferedReader(new FileReader(descrFile));
                String l = null;
                while ((l = r.readLine()) != null) {
                    buffer.append(l + "\n");
                }
                r.close();
                JEditorPane area = new JEditorPane();
                area.setContentType("text/html");
                area.setText(buffer.toString());
                area.setEditable(false);
                area.addHyperlinkListener(this);
                area.setCaretPosition(0);
                p.add(new JScrollPane(area));
            } catch (IOException e) {
                LOGGER.error("Error reading description file: " + descrFile.getPath(), e);
            }
        }
    }

    p.validate();
    p.repaint();
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Convert a html code to an image/*from   w ww .  ja v  a2  s. c  om*/
 * 
 * @param html html to convert
 * @return html converted to png
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected byte[] htmlToImage(String html) throws IOException, PPTGeneratorException {
    try {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        editor.setSize(editor.getPreferredSize());
        editor.addNotify();
        LOGGER.debug("Panel is built");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        return out.toByteArray();
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Add an image with a html code without change its dimension
 * /*w  w w.  j  a  v  a2 s. com*/
 * @param slideToSet slide to set
 * @param html html code
 * @param x horizontal position
 * @param y vertical position
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected void addHtmlPicture(Slide slideToSet, String html, int x, int y)
        throws IOException, PPTGeneratorException {
    try {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        if (!ge.isHeadlessInstance()) {
            LOGGER.warn("Runtime is not configured for supporting graphiv manipulation !");
        }
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        LOGGER.debug("Editor pane is built");
        editor.setSize(editor.getPreferredSize());
        editor.addNotify(); // Serveur X requis
        LOGGER.debug("Panel rendering is done");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        LOGGER.debug("image is written");
        addPicture(slideToSet, out.toByteArray(),
                new Rectangle(x, y, editor.getPreferredSize().width, editor.getPreferredSize().height));
        LOGGER.debug("image is added");
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:com.evanbelcher.DrillBook.display.DBMenuBar.java

/**
 * Displays about window.//  w  w w. ja va2s. c o  m
 */
private void about() {
    String msg = "<html>DrillBook " + State.VERSION
            + "<br>Created by Evan Belcher, 2016<br><a href=\"https://github.com/EbMinor3/DrillBook\">GitHub</a><br><a href=\"http://evanbelcher.com\">Website</a><br><br><a href=\"https://icons8.com\">Icon pack by Icons8</a></html>";
    JEditorPane editorPane = new JEditorPane("text/html", msg);
    editorPane.setEditable(false);
    editorPane.setBackground(new JLabel().getBackground());
    editorPane.addHyperlinkListener(e -> {
        if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
            try {
                Desktop.getDesktop().browse(e.getURL().toURI());
            } catch (IOException | URISyntaxException e1) {
                e1.printStackTrace();
            }
        }
    });

    if (!(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE))) {
        editorPane.setContentType("text");
        editorPane.setText("DrillBook " + State.VERSION
                + "\nCreated by Evan Belcher, 2016\nGitHub: https://github.com/EbMinor3/DrillBook\nWebsite: http://evanbelcher.com\n\nIcon pack by Icons8: https://icons8.com");
    }

    JOptionPane.showMessageDialog(this, editorPane, "About", JOptionPane.PLAIN_MESSAGE);
}