Solved

image in html text in JEditorPane

Posted on 2004-10-16
8
301 Views
Last Modified: 2012-06-21
Hi experts,

I'm having trouble displaying images in my the middle of some html text in a editorpane
please give me an example for this

thanks
0
Comment
Question by:Samooramad
  • 6
  • 2
8 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12329933
Your Html Should be some thing like this , to make image centered in editorpane

<body>

<table border="0" width="100%" height="299">
  <tr>
    <td width="24%" height="48"></td>
    <td width="50%" height="48">
      <p align="center"><b><font face="Arial Black" size="5">Centered Image</font></b></td>
    <td width="26%" height="48"></td>
  </tr>
  <tr>
    <td width="24%" height="101"></td>
    <td width="50%" height="101">
      <p align="center"><img border="0" src="file:///C:/Documents%20and%20Settings/Sudhakar/My%20Documents/pic88122.jpg" width="160" height="160"></td>
    <td width="26%" height="101"></td>
  </tr>
  <tr>
    <td width="24%" height="132"></td>
    <td width="50%" height="132"></td>
    <td width="26%" height="132"></td>
  </tr>
</table>

</body>
0
 

Author Comment

by:Samooramad
ID: 12329984
>>file:///C:/Documents%20and%20Settings/Sudhakar/My%20Documents/pic88122.jpg

yu mean I have to include the whole path? I wasn't doing that.. by the way..what is the % for?
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 200 total points
ID: 12330003
 FlowLayout borderLayout1 = new FlowLayout();
  JButton buttons[] = new JButton[10];
  JButton oldbutton;
  JEditorPane pane = new JEditorPane();


  void jbInit() throws Exception {
    this.getContentPane().setLayout(new BorderLayout());
    JPanel panel1 = new JPanel();
    panel1.setLayout(borderLayout1);
    try {
      String url = "http://java.sun.com";
      JEditorPane editorPane = new JEditorPane("text/html",htmlText);
      editorPane.setEditable(false);

      panel1.add(editorPane);
      getContentPane().add(panel1, BorderLayout.CENTER);

    }
    catch (Exception e) {
    }

    this.getContentPane().add(panel1);

  }

  public static void main(String[] args) {
    ButtonTest buttonTest = new ButtonTest();
    buttonTest.setSize(500, 500);
    buttonTest.show();
  }


  private String htmlText="<body> <table border='0' width='100%' height='410'>   <tr>     <td width='24%' height='73'></td>     <td width='50%' height='73'>       <p align='center'><b><font face='Arial Black' size='5'>Centered Image</font></b></td>     <td width='26%' height='73'></td>   </tr>   <tr>     <td width='24%' height='187'></td>     <td width='50%' height='187'>       <p align='center'><img border='0' src='file:///C:/Documents%20and%20Settings/Sudhakar/My%20Documents/pic88122.jpg' width='160' height='160'></td>     <td width='26%' height='187'></td>   </tr>    <tr>     <td width='24%' height='132'></td>     <td width='50%' height='132'></td>      <td width='26%' height='132'></td>   </tr> </table>   </body>  ";
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12330009
>>yu mean I have to include the whole path?

Then how u are loading the image??
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12330031
>>yu mean I have to include the whole path?

Not necessarily full path.
If image is known loacation

You can format the html text some thing like below

File file=new File(str);
String filepath=file.getAbsoltePath().toString();

private String htmlText="<body> <table border='0' width='100%' height='410'>   <tr>     <td width='24%' height='73'></td>     <td width='50%' height='73'>       <p align='center'><b><font face='Arial Black' size='5'>Centered Image</font></b></td>     <td width='26%' height='73'></td>   </tr>   <tr>     <td width='24%' height='187'></td>     <td width='50%' height='187'>       <p align='center'><img border='0' src='file:///";

htmlText=htmlText+filepath;

htmlText=htmlText+" width='160' height='160'></td>     <td width='26%' height='187'></td>   </tr>    <tr>     <td width='24%' height='132'></td>     <td width='50%' height='132'></td>      <td width='26%' height='132'></td>   </tr> </table>   </body>  ";


0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12330124
package org.prithvi.test;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.awt.*;
import javax.swing.*;

class ImageTest
    extends JFrame {
  FlowLayout borderLayout1 = new FlowLayout();
  JEditorPane pane = new JEditorPane();

  ImageTest() throws Exception {
    jbInit();
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(new BorderLayout());
    JPanel panel1 = new JPanel();
    panel1.setLayout(borderLayout1);
    try {
      String url = "http://java.sun.com";
      JEditorPane editorPane = new JEditorPane("text/html", htmlText);
      editorPane.setEditable(false);

      panel1.add(editorPane);
      getContentPane().add(panel1, BorderLayout.CENTER);

    }
    catch (Exception e) {
    }

    this.getContentPane().add(panel1);

  }

  public static void main(String[] args) throws Exception {
    ImageTest imageTest = new ImageTest("c:/a.jpg");
    imageTest.setSize(500, 500);
    imageTest.show();
  }

  private String htmlText = "";

  /**
   * ImageTest
   *
   * @param string String
   */
  public ImageTest(String string) throws Exception {
   
    this.path = string;

    htmlText = htmlText + "<body> \r\n<table border='0' width='100%' height='410'>  \r\n <tr>     <td width='24%' height='73'></td>     <td width='50%' height='73'>    \r\n   <p align='center'><b><font face='Arial Black' size='5'>Centered Image</font></b></td>    \r\n <td width='26%' height='73'></td>   </tr>  \r\n <tr>     <td width='24%' height='187'></td>     <td width='50%' height='187'>    \r\n   <p align='center'>\r\n<img border='0' src=";
    htmlText = htmlText + "'file:///" + new java.io.File(path) + "' width='160' height='160'></td>    \r\n <td width='26%' height='187'></td>   </tr>    <tr>     <td width='24%' height='132'></td>     <td width='50%' height='132'></td>      \r\n<td width='26%' height='132'></td>   </tr> </table>   </body>  ";
     jbInit();
     JOptionPane.showMessageDialog(null,htmlText);

  }

  private String path = "";

}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12330128
Check the above Example

Workine fine at my end
0
 

Author Comment

by:Samooramad
ID: 12330177
yeah it worked..that must have been my problem :)

thank you
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now