Example usage for javax.swing JEditorPane getPreferredSize

List of usage examples for javax.swing JEditorPane getPreferredSize

Introduction

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

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Returns the preferred size for the JEditorPane.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("EditorPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {//  w ww. j a  v a  2  s .  c o  m
        JEditorPane editorPane = new JEditorPane("http://www.java2s.com");
        editorPane.setEditable(false);
        Dimension dimension = editorPane.getPreferredSize();

        JScrollPane scrollPane = new JScrollPane(editorPane);
        frame.add(scrollPane);
    } catch (IOException e) {
        System.err.println("Unable to load: " + e);
    }

    frame.setSize(640, 480);
    frame.setVisible(true);
}

From source file:com.rapidminer.gui.flow.processrendering.annotations.AnnotationDrawUtils.java

/**
 * Calculates the preferred height of an editor pane with the given fixed width for the
 * specified string./*  w ww. j  a  v  a 2  s. c  o m*/
 *
 * @param comment
 *            the annotation comment string
 * @param width
 *            the width of the content
 * @return the preferred height given the comment
 */
public static int getContentHeight(final String comment, final int width) {
    if (comment == null) {
        throw new IllegalArgumentException("comment must not be null!");
    }
    JEditorPane dummyEditorPane = new JEditorPane("text/html", "");
    dummyEditorPane.setText(comment);
    dummyEditorPane.setBorder(null);
    dummyEditorPane.setSize(width, Short.MAX_VALUE);

    // height is not exact. Multiply by magic number to get a more fitting value...
    if (SystemInfoUtilities.getOperatingSystem() == OperatingSystem.OSX
            || SystemInfoUtilities.getOperatingSystem() == OperatingSystem.UNIX
            || SystemInfoUtilities.getOperatingSystem() == OperatingSystem.SOLARIS) {
        return (int) (dummyEditorPane.getPreferredSize().getHeight() * 1.05f);
    } else {
        return (int) dummyEditorPane.getPreferredSize().getHeight();
    }
}

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

/**
 * Convert a html code to an image//from   w  ww .j a 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
 * //from   w  ww.j av  a  2  s.c o m
 * @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:net.yacy.cora.util.Html2Image.java

/**
 * render a html page with a JEditorPane, which can do html up to html v 3.2. No CSS supported!
 * @param url/*  www  .j  a v a  2 s . c o  m*/
 * @param size
 * @throws IOException 
 */
public static void writeSwingImage(String url, Dimension size, File destination) throws IOException {

    // set up a pane for rendering
    final JEditorPane htmlPane = new JEditorPane();
    htmlPane.setSize(size);
    htmlPane.setEditable(false);
    final HTMLEditorKit kit = new HTMLEditorKit() {

        private static final long serialVersionUID = 1L;

        @Override
        public Document createDefaultDocument() {
            HTMLDocument doc = (HTMLDocument) super.createDefaultDocument();
            doc.setAsynchronousLoadPriority(-1);
            return doc;
        }

        @Override
        public ViewFactory getViewFactory() {
            return new HTMLFactory() {
                @Override
                public View create(Element elem) {
                    View view = super.create(elem);
                    if (view instanceof ImageView) {
                        ((ImageView) view).setLoadsSynchronously(true);
                    }
                    return view;
                }
            };
        }
    };
    htmlPane.setEditorKitForContentType("text/html", kit);
    htmlPane.setContentType("text/html");
    htmlPane.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
        }
    });

    // load the page
    try {
        htmlPane.setPage(url);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // render the page
    Dimension prefSize = htmlPane.getPreferredSize();
    BufferedImage img = new BufferedImage(prefSize.width, htmlPane.getPreferredSize().height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = img.getGraphics();
    htmlPane.setSize(prefSize);
    htmlPane.paint(graphics);
    ImageIO.write(img, destination.getName().endsWith("jpg") ? "jpg" : "png", destination);
}