JEditorPane bachground image

Hi,
i'am implementing a WYSYWIG HTML editor in Swing.
Question: How can i set a HTML <BODY> backgound Image ? And How can a change the Image ?
thx a lot.

Hi,
the answer still is as outlined in my post to your previous question, see http://forum.java.sun.com/thread.jsp?forum=57&thread=377594
Ulrich

Similar Messages

  • JEditorPane/HTML image loading delay

    I am using a JEditorPane to render HTML to a graphics buffer which I then write as an image file. Since HTML loading is asynchronous I wait for the "page" property change event after calling setPage() before using print() to capture the rendered HTML to a graphics buffer. Unfortunately, the "page" property change event is fired after the HTML is loaded but before images referenced by the HTML are rendered. As a result, the image that I capture has little picture icons in place of the graphics. If I sleep for a short time before capturing the image, it works fine. However, sleeping is not an acceptable solution as there is no way to predict how long I will need to sleep because it varies based on system load and complexity of the HTML being rendered. Is there any way to check if an HTML document has completely loaded including all the images? Does anyone have a better way to capture an image of a rendered HTML document that would not have this problem?
    Below is a code snippet that shows what I am trying to do.
    static final JEditorPane p;
    static final Object done;
    static {
        done=new Object();
        p=new JEditorPane();
        p.setSize(1728, 2156);
        p.addPropertyChangeListener("page", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) {
            synchronized(done) {
                done.notifyAll();
    static void capture(File htmlFile) {
        BufferedImage img=new BufferedImage(1728, 2156, BufferedImage.TYPE_BYTE_GRAY);
        synchronized(done) {
            p.setPage(htmlFile.toURL());
            try {
                done.wait();
            } catch(InterruptedException e) {}
    /* without this code images are not rendered
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {}
        Graphics g=img.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 1728, 2156);
        p.print(g);

    HI Sarnoth,
    I'm trying to do something similar and have used your code, but it doesn't work completely for me. Do you see anything that I'm doing wrong? I am trying to save the HTML page as a JPEG image. This is my code and I'm having problems. Anyone have any ideas? It is dying on the je.encode(bi) line. Also, for some reason, if I uncomment the commented out lines, it hangs. Any ideas on that one too?
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import javax.swing.JEditorPane;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageEncoder;
    import com.sun.media.jai.codec.JPEGEncodeParam;
    public class HTMLPageToImageConverter
         private HTMLPageToImageConverter() {
         static final JEditorPane p;
         static final Object done;
         static {
             done = new Object();
             p = new JEditorPane();
             p.setSize(1728, 2152);
    /*         p.addPropertyChangeListener("page", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) {
                 synchronized(done) {
                     done.notifyAll();
         static void GenerateHTMLBufferedImage(File htmlFile, BufferedImage img) {
             img = new BufferedImage(1728, 2152, BufferedImage.TYPE_BYTE_GRAY);
             synchronized(done) {
                 try {
                      p.setPage(htmlFile.toURL());
                 } catch(IOException e1) {}
    /*             try {
                     done.wait();
                 } catch(InterruptedException e) { System.out.println("GenerateHTMLBufferedImage InterruptedException"); }
             Graphics g = img.createGraphics();
             g.setColor(Color.white);
             g.fillRect(0, 0, 1728, 2152);
             p.print(g);
         public static boolean SaveHTMLFileToJPEG(String htmlFile, String jpegFile) {
              try {
                   BufferedImage bi = null;
                   GenerateHTMLBufferedImage(new File(htmlFile), bi);
                   FileOutputStream fos = new FileOutputStream(jpegFile);
                   JPEGEncodeParam jp = new JPEGEncodeParam();
                   ImageEncoder je = ImageCodec.createImageEncoder("JPEG", fos, jp);
                   je.encode(bi);
                   fos.close();
              } catch(FileNotFoundException fnfe) { System.out.println("SaveHTMLFileToJPEG FileNotFoundException"); }
                catch(IOException ioe) { System.out.println("SaveHTMLFileToJPEG IOException"); }
              return true;
    }Called from other code
    HTMLPageToImageConverter.SaveHTMLFileToJPEG("d:/page1.html", "d:/page1.jpeg");
    ...Thanks in advence,
    --Ed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • JEditorPane HTMLEditorkit Image align left

    Hi,
    I am developing a HTML Editor with JEditorPane and HTMLEditorKit. Apart from the many other difficulties, the HTMLEditorKit doesn't show a text next to an image correctly. I set an Image alignment to left and hopped, the text would be displayed text to it beginninge at the top. But it doesn't. Can anyone help.
    Christian

    Hi,
    I'm sorry, but this doesn't help me at all. To be more precisely, I want a text placed next to an image. A normal Browser does is when you say something like <img align="left" ...> and just place your text behind this tag. It looks like this:
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    %%%IMAGE%%%% My Text My Text My Text My Text My Text My Text
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    The Problem with the JEditorpane and the HTMLEditorKit is, that it shows up like this:
    %%%IMAGE%%%%
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    If I put the alignment to top or something else, I can let it look like this:
    %%%%%%%%%% My Text My Text My Text My Text My Text My Text
    %%%IMAGE%%%%
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    My Text My Text My Text My Text My Text My Text
    But annoyingly I let it show up like in any normal Internet Browser.
    Hope my problem is more clearly now and somebody can help me.
    Yours sincerely,
    Christian

  • JEditorPane displaying "image" arrow-icon

    windows 1.4.1
    using a JEditorPane to display html.
    little icons with the word "image" in an arrow pointing left are sprinkled through the page rendering. any ideas on what this is about?
    comments:
    i render the same URL in two JeditorPanes. in the first it paints successfully (no arrow icons). in the latter it gets the arrow icons.
    successful rendering JeditorPane is in a JFrame.
    bizarre rendering is in a JWindow.
    thanks

    i set the editorpane to setEditable(false) and the arrow icons went away.

  • How JEditorPane render images

    hello
    my JEditorPane is displaying pages well but images are not shown properly.
    how EditorPane will display the images properly
    i think image path is need to be rendered properly .
    any help will be appreciated
    thanx

    thanx for help
    i m not using setIcon().i think it will be used if display only image
    i m using ViewFactory, subclass of HTMLEditorKit
    and also create(Element elem) method to render images
    code is running well but not displaying image

  • Jeditorpane/html - images disappear when scrolling

    The title says it-
    I've had this issue with JEditorPanes in my own code and also with the Java Help viewer. I'm using 1.4.1_01.
    I don't have a lot of images or any very large ones. My pages are quite standard, and very simple- just text and greatly reduced screenshots.
    When I scroll up and down in the component, however, images either completely disappear or part of them disappears. Reloading the page fixes it. Definately a repaint issue of some sort. But, in the case of Java Help, I don't have access to the JEditorPane to hack in any kind of fix.
    Whats the deal here? I can't find any bug postings about this one but it certainly seems to be a major bug.
    Thanks

    I'm glad to hear someone else is having the problem. Any ideas on a work around?
    There has to be some work around. For example, I'm using IDEA for my IDE and it is running under 1.4.1_01- clearly they have used Java Help but their help view isn't having the same problem mine is. I hope I find a solution soon. This is an absolute show stopper. I can't deploy something that is clearly broken.
    It is next to impossible that this is a user error- I haven't even written any code! It is all Java Help's code. I've even tried to turn off all the fancy drawing that is part of Swing, and turned off all hardware acceleration in Windows.

  • JEditorPane and images..

    hi guyss..
    i am trying to display an img within the JtextPane(or alternatively JEditorPane)...can any one of u tell me the best way to do it...
    its like this..
    i 've 3 JEditorPanes say pane1, 2, 3..i display 2 hyperlinks in pane1. it shows the html content with 2 more hyperlinks...when u click on one of these hyperlinks, i use pane2.setPage() to display the contents of the html file in pane2. the 2nd hyperlink is actually an img file. so when i click on this hyperlink, i want pane3 2 display the img. the html part in pane2 seems 2 wk fine but the img is nor correctly displayed in pane3.
    've tried 2 include the path 2 the img file and usingJTextPane.setText(<img>) but it doesnt display the img...
    can any1 suggest how 2 go abt it...
    thks
    siriiuss

    Hi Ruben,
    you will have to implement adjustment of table cells yourself to achieve this. With the existing way JEditorPane et. al. render tables table cells always are sized either in default sizes or by attributes explicitly giving the size (such as attribute WIDTH).
    Ulrich

  • How to set a jpg image ContentType of JEditorPane

    Hi how can i display an image on a JEditorPane like image.jpg?
    I tried myEditor.setContentType("image/jpeg") but it still displays characters instead of the image.
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    public class Pagina extends JFrame implements Runnable {
         JEditorPane e;
         public Pagina(){
              super("fds");
              e = new JEditorPane();
              e.setEditable(false);
              e.setContentType("image/jpeg");
              Container c = getContentPane();
              c.add(e);
              setSize(400,400);
              setVisible(true);
              Thread t = new Thread(this);
              t.start();
         public void run(){
              while(true){
                   try{
                   e.setPage( "http://10.0.0.5/SnapShot.jpg" );
                   Thread.sleep(1000);
              catch(IOException ioe){}
              catch(InterruptedException ie){}
         public static void main (String [] args) {
              new Pagina();
    }

    In your current app, you are trying to redisplay an image in some infinite loop. This smells bad to me. I am no expert in java graphics, far from it, but if you want to paint a background image within a JEditorPane, since JEditorPane is a component and has to paint itself, you could override its paintComponent method and draw an image inside there using the Graphics object.
    another way is to put the JEditorPane on top of a JPanel that draws your picture. In both instances, I think that you'll need to set the JEditorPane's opaqueness to "false".
    Something like so, perhaps?
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    class ImageJEditorPane extends JPanel
        private static final String IMAGE_FILE1 = "disc/d1/f/image1.jpg";   
        private static final String IMAGE_URL = "http://10.0.0.5/SnapShot.jpg";
        private BufferedImage myImage = null;
        private JEditorPane editorPane;
        ImageJEditorPane()
            try
                setPreferredSize(new Dimension(800, 650));
                setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
                setLayout(new BorderLayout());
                //myImage = ImageIO.read(new File(IMAGE_FILE1));
                myImage = ImageIO.read(new URL(IMAGE_URL));
                editorPane = new JEditorPane()
                    @Override
                    protected void paintComponent(Graphics g)
                        g.drawImage(myImage, 0, 0, null);
                        super.paintComponent(g);
                editorPane.setPreferredSize(new Dimension(1200, 1000));
                editorPane.setOpaque(false);
                JScrollPane scrollPane = new JScrollPane(editorPane);
                add(scrollPane, BorderLayout.CENTER);
            catch (IOException e)
                e.printStackTrace();
         //or you could do it this way
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            g.drawImage(myImage, 0, 0, null);
        private static void createAndShowGUI()
            JFrame frame = new JFrame("ImageJEditorPane Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new ImageJEditorPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
    }Edited by: Encephalopathic on Dec 21, 2007 3:09 PM

  • How to prevent JEditorPane to show remote images

    Hello
    I make an e-mail client with JEditorPane to show the html content.
    If the content contains IMG tag with src like http://somehost.on.the.web...
    then JEditorPane tries to load the remote image. But I have an internet connection via proxy. So, JEditorPane cannot find somehost.on.the.web on the LAN and an annoying timeout occurs. If I use System.setProperty("http.proxySet","true"), ... then JEditorPane uses proxy and loads images. I do not want to use a proxy. But how can I eliminate the timeout? It doesn't occur in Linux. In windows it doesn't occur only if there are no active network connections.
    I learn that JEditorPane loads images throgh the java.awt.Toolkit.getDefaultToolkit()
    ImageView.loadImage()
    Could you, please, tell me how can I force JEditorPane to do not load remote images or eliminate the problem with timeout in Windows with LAN.

    > I have the first option checked.
    I wouldn't use that option, nor would I suggest anyone else
    use it UNLESS
    you are supremely confident in your ability to not make
    mistakes.
    Otherwise, it's a train wreck waiting to happen.
    > Yes...I DO have check in and check out enabled in the
    Sharing Files, Part
    > 2
    > box.
    Which is why you are being prompted to overwrite your local
    copy. Are you
    the only one working on that site? If so, you do not need, or
    even want the
    CI/CO option.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    ==================
    "kabra001" <[email protected]> wrote in
    message
    news:fto4ur$c6b$[email protected]..
    > Thanks for the response Murray
    >
    > In the site definition box Editinf Files Part 3 there
    are 2 options .....
    > 1.Edit local copies on my machine, then upload when
    ready.
    > 2.Edit directly on server using local network
    > I have the first option checked.
    > This does not seem to be automatic upload on save
    option,....
    > however...its
    > when I OPEN the LOCALfile that it seems to log in to the
    server and GET
    > the
    > remote copy, not present the local copy for editing.
    >
    > Yes...I DO have check in and check out enabled in the
    Sharing Files, Part
    > 2
    > box.
    >
    > cheers
    >
    >

  • JEditorPane not showing bmp images

    Hey.
    I created a local html file that displayed some images. The jpegs are shown correctly, but the bmps are not shown. Does anybody know why?
    This is where I instantiate the JEditPane:
    JEditorPane htmlPane = new JEditorPane(new URL("file:///"+curFilePath));The URL points to a file in my machine with a html tag and a bunch of img tags.
    Thanks,
    Andre

    Bitmaps are not supported by core Java. You need an extension to read/write bitmaps (like JAI or ImageIO). Then, you have to override the HTMLEditorKit to return a custom ViewFactory that loads bitmap images. Not necessarily a difficult task, but you'll need to dig into the underlying classes to make sure you do it right.
    Message was edited by:
    Jaspre

  • Need help.. displaying images with setText on a JEditorPane

    Does anybody know how to embed an image into html text when using the JEditorPane.setText?
    The images are stored in a jar file, and we know how to retrieve them and store them in an Image object from getDefaultToolkit().getImage()
    Sample codes would be really nice, but any answers will be greatly appreciated. Thanks!

    The following is from some code I worked on a while ago, seemed to work fine however this was only a test tool so I didnt test it fully!
    public static final java.net.URL ERROR_ICON = ClassLoader.getSystemResource ("xmlviewer/res/error.gif");
    // some code....
    public void reportError (String msg) {
            String err = "<img src=" + ERROR_ICON.toString() + "> " +
                         makeHTMLFriendlyString(msg) + "<br>";
            reportList.append (err);
            jTextAreaParseErrors.setText (  reportList.toString() );
    }Obviously you will have to put your own image path into the .getSystemResource call. Hope this helps!
    Jon

  • Scrolling with images in a jeditorpane

    Hi,
    I'm really struggling with a jeditor pane. I'm using it to show formatted system messages in the interface i'm writing. In some cases these messages may include an image (usually a reference to an online image using html <img> tags.)
    What I want to do is automatically scroll to the bottom of the pane when new text is added. If i use only html formatted text this works fine, but as soon as there is an image in there it doesn't scroll far enough. I believe what's happening is that the image has not finished loading so it scrolls only the length of the placeholder.
    This is the code i'm using to scroll:
    JEditorPane IMPane;
    JScrollPane IMScroller;
    //...snip...
    private void scrollPaneToBottom(){
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    if (isAdjusting()){
                        return;
                    int height = IMPane.getHeight();
                    IMPane.scrollRectToVisible(new Rectangle(0, height - 1,1, height));
        private boolean isAdjusting(){
            JScrollBar scrollBar = IMScroller.getVerticalScrollBar();
            if (scrollBar != null && scrollBar.getValueIsAdjusting()){
                return true;
            return false;
        }I have tried various options including moving the caret but to no avail. Perhaps there is some way i can wait till the page is fully loaded or something like that?
    I hope somebody can help me with this - it's driving me bananas.
    Thanks in advance,
    Crystal

    In the future, Swing related questions should be posted in the Swing forum.
    I would guess your isAdjusting() method is returning true, so the code never executes.
    An easier way to scroll to the bottom is to use:
    textComponent.setCaretPosition( textComponent.getDocument().getLength() );

  • Displaying local image file in JEditorPane with relative path

    Hi All,
    I am showing an html inside JEditorPane. I have set its content type to text/html. The code goes like this,
    StringBuffer bf=new StringBuffer();
    bf.append("<html><head></head><body>");
    bf.append("<img src=\"calendar.gif\" width=\"10\" height=\"10\" border=\"0\">");
    bf.append(" <font face=\"Verdana\" size=\"3\">");
    bf.append("Hi All");
    bf.append("</font><br>");
    bf.append("</body></html>");
    jeditorpane.setContentType("text/html");
    jeditorpane.setText(bf.toString());
    The image calendar.gif is present at the base directory of the package. But still it is not shown in JEditorPane. But if i give full path like,
    <img src="file://C:/man/calendar.gif">
    then image appears.
    Any idea like what is the document root for JEditorPane??
    Thanks in Advance,
    Manjinder Singh

    One solution is to dynamically set the base href
    String s = "test.txt"; // "dummy" file in current directory
    File f = new File(s);
    s = f.getAbsolutePath();
    s = "file:\\"+s;
    p("<BASE HREF='" + s + "'>");

  • JEditorPane not showing image

    hiii
    I have made a JEditorPane which is accepting a url but the background image hides below it when painted although i have set the opaque option to false
    whats the problem here i attach the code
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.*;
    import java.net.*;
    public class TextImage
         public static void main(String args[]) throws MalformedURLException
              final String sPath = System.getProperty( "user.dir" ) + "/";
              URL url = new URL( "file:///"+ sPath +"help.html" );
              JFrame frm = new JFrame("Text Demo.");
              frm.setSize(1200, 600);
              MyText txt = new MyText(url);
              frm.add(new JScrollPane(txt));
              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frm.pack();
              frm.setVisible(true);
    class MyText extends JEditorPane
         ImageIcon img = new ImageIcon("cs4.jpg");
         public MyText(URL url)
              super();
              this.url = url;
              try
                   setPage(url);
                   setForeground(Color.white);
                   setOpaque(false);
              catch( MalformedURLException e )
                  System.out.println( "Malformed URL: " + e );
              catch( IOException e )
                  System.out.println( "IOException: " + e );
         public boolean isOpaque()
              return false;
         public void paint(Graphics g)
              img.paintIcon(this , g, 0,0);
              super.paint(g);
         private URL url;
    }

    Please see the following URL :
    http://www.java2s.com/Code/Java/Swing-JFC/JEditorPaneExample16.htm

  • HTML of image into JEditorPane

    I was wondering is it possible put HTML text into JEditorPane which contains image so that image can be viewed into JEditorPane.
    This is my code:
    JEditorPane jep = new JEditorPane();
              jep.setContentType("text/html");
              jep.setText("<html><img src=\"SomeImage.bmp\"/></html>");
              jep.setEditable(false);Did I write wrong HTML code into setText() method?
    How to put image with HTML code into JEditorPane?

    Works for me -- maybe you are having path problems:
    import java.io.*;
    import javax.swing.*;
    public class Test {
    public static void main(String[] args) throws
    IOException {
    String text = "<html><body><img
    src=\"http://today.java.net/jag/bio/JagHeadshot.jpg\"
    </img></body></html>";
    JEditorPane editorpane = new
    JEditorPane("text/html", text);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new
    JScrollPane(editorpane));
    f.setSize(800,600);
    f.setVisible(true);
    }Also: why the requirement to use JEditorPane? There
    are simplier ways to display an image!Your String text variable uses as image source URL link. I use local file.
    Well, it's not strict requirement for me to use JEditorPane for displaying image. In fact, you could tell me some simpler ways?

Maybe you are looking for

  • Iscsitarget installation fails on Windows 2008 R2 server

    Dear All We have windows 2008 Server which is also DC server. I am trying to install iscsitarget utility which I downloaded from Microsoft site. I want to configure the server as iscsi target and map the disk space to another server as disk volume. T

  • IPhone 3G not responding after upgrading firmware

    I've been given an old iPhone 3G 16GB but as it hasn't been used for a while, the firmware on it was 2.8.?? something. I have the latest version of iTunes so I connected it to upgrade so I could download apps as nearly all of them needed iOS 3+. It w

  • Please help me with UTF-8 in a institutional server

    Hi, I know this thread has been treated a lot of times but I have a question about the server wich I have to upload the files on the server is on Institutional facilities where I have no access, no info from webmaster, even to the server specificatio

  • Can file name get from partial field value ?

    Hi, Friend: I have file receiver with FCC, I used variable substitution (I have to use this rather than dynamic configuration, since there are multiple files created by multi-mapping), so the file name is pass via payload to adapter. In advanced tab,

  • Boris Title 3D in FCE

    I'm using FCE 4.0.1 and Boris Title 3D version 2.1.0.060120 I'm finding that text justification doesn't work correctly. Also, it doesn't allow me to set it differently in different sections the way you can change size or color & stuff. This is the on