Solved

image in html text in JEditorPane

Posted on 2004-10-16
8
302 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

914 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

18 Experts available now in Live!

Get 1:1 Help Now