Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

image in html text in JEditorPane

Posted on 2004-10-16
8
Medium Priority
?
316 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

824 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