Solved

image in html text in JEditorPane

Posted on 2004-10-16
8
304 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
throw exception 21 44
maven module vs maven project 3 23
Chrome and Firefox Java 5 31
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

773 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