?
Solved

image in html text in JEditorPane

Posted on 2004-10-16
8
Medium Priority
?
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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