Swing - HTML content in JEditroPane

Object o = messages[msgno].getContent();

if(o!=null){

if (oinstanceof String){

txtcontent.setText((String)o);

}elseif (oinstanceof Multipart){

System.out.println("This is a Multipart");

Multipart mp = (Multipart)o;

int count = mp.getCount();

for (int i = 0; i < count; i++){

txtcontent.setText(txtcontent.getText() +"\n" + mp.getBodyPart(i).getContent());

}

}

}

Above is the code I have written for the getting content.and I am using JEditorPane to display it. - txtcontent is JEditorPane

It showing somethig like :

Hello Sumant,

This is HTML content Page

Use Editor Pane To display this Message

<HTML><BODY>

Hello Sumant,

This is HTML content Page

Use Editor Pane To display this Message
</BODY></HTML>

the message is actually a output of below HTML script but it showing me the HTML script.

What is the problem and what should be done

Please Reply

thank you

[1876 byte] By [JTable_need_infoa] at [2007-11-26 23:10:46]
# 1
can you post the part where you instantiated your JEditorPane object?
lem@phila at 2007-7-10 14:07:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

class showMessage extends JFrame{

private boolean textIsHtml = false;

public showMessage(){

super("Message");

try

{

setLayout(null);

int msgno = messages.length-messageTable.getSelectedRow()-1;

messages[msgno].setFlag(Flags.Flag.SEEN, true);

JLabel from = new JLabel();

from.setText(messages[msgno].getFrom()[0].toString());

noOfUnreadMessage-=1;

foldernmpane.invalidate();

lblFoldernm.setText(" Folder : " + folder.getName() + " ( " + noOfUnreadMessage + " / " + messages.length + " )");

foldernmpane.validate();

from.setBorder(new TitledBorder("From : "));

from.setBounds(5,1,400,40);

JLabel to = new JLabel();

to.setText( messages[msgno].getAllRecipients()[0].toString());

to.setBorder(new TitledBorder("To :"));

to.setBounds(5,40,400,40);

JLabel sub = new JLabel();

sub.setText( messages[msgno].getSubject().toString());

sub.setBorder(new TitledBorder("Subject : "));

sub.setBounds(5,80,400,40);

JLabel rcvdate = new JLabel();

rcvdate.setText( messages[msgno].getReceivedDate().toString());

rcvdate.setBorder(new TitledBorder("Received Date : "));

rcvdate.setBounds(5,120,400,40);

JLabel attach = new JLabel();

attach.setText( messages[msgno].ATTACHMENT);

attach.setBorder(new TitledBorder("Attachment :"));

attach.setBounds(410,1,300,40);

//JTextArea txtcontent = new JTextArea();

JEditorPane txtcontent = new JEditorPane();

txtcontent.setEditable(false);

//Multipart mp = (Multipart)messages[msgno].getContent();

//int count = mp.getCount();

// BodyPart body_part;

// for (int i = 0; i < count; i++){

Object o = messages[msgno].getContent();

if(o!=null){

if (o instanceof String) {

txtcontent.setText((String)o);

} else if (o instanceof Multipart) {

System.out.println("This is a Multipart");

Multipart mp = (Multipart)o;

int count = mp.getCount();

for (int i = 0; i < count; i++) {

txtcontent.setText(txtcontent.getText() + "\n" + mp.getBodyPart(i).getContent());

// printParts(mp.getBodyPart(i));

}

}

}

/*String t1 = getText((Part)(messages[msgno].getContent()));

txtcontent.setText(t1);

*/

//}

/* if(mp.getBodyPart(i).getContentType().e)

txtcontent.setText(txtcontent.getText() + "\n" + mp.getBodyPart(i).getContentType());

//txtcontent.setText(messages[msgno].getContent().toString());

//System.out.println(messages[msgno].getContent().toString());

*/

/* DataHandler dh = messages[msgno].getDataHandler();

CommandInfo cinfo = dh.getCommand("view");

Component comp = (Component) dh.getBean(cinfo);

this.setMainViewer(comp);

*/

JScrollPane pane = new JScrollPane(txtcontent);

pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

pane.setBorder((new TitledBorder("Message")));

pane.setBounds(5,165,700,500);

add(from);

add(to);

add(sub);

add(rcvdate);

add(attach);

add(pane);

//JScrollPane pane = new JScrollPane(txtcontent);

//pane.setBorder(new TitledBorder("Message"));

//add(pane);

//pane.setBounds(1,1,500,500);

setSize(715,700);

setResizable(false);

setLocationRelativeTo(null);

setVisible(true);

// getMessages(folder.getName());

// messageTable.setModel(model);

}catch(Exception e){System.out.println(e);}

}

JTable_need_infoa at 2007-7-10 14:07:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

You can try with next source code just use a simple HTML & add it inside the JPane, make sure the html page inside same folder.

/**

*

* @author Carlo Vandyko

* http://www.cs.buap.mx/~carlo

*

*/

import java.io.*;

import java.sql.*;

import java.awt.*;

import java.net.*;

import java.util.*;

import javax.swing.*;

import javax.swing.text.*;

import java.awt.event.*;

public class Tutorial extends JInternalFrame {

public Tutorial() {

initComponents();

setSize(500,200);

}

private void initComponents() {

setClosable(true);

setIconifiable(true);

setFrameIcon(new ImageIcon("tut.png"));

getContentPane().setLayout(null);

setTitle("Tutorial del Software");

JTextPane panel = new JTextPane();

JEditorPane editorPane = createEditorPane();

JScrollPane paneScrollPane = new JScrollPane(editorPane);

paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

paneScrollPane.setPreferredSize(new Dimension(250, 155));

getContentPane().add(paneScrollPane);

paneScrollPane.setBounds(0, 0, 480, 160);

try{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}catch(Exception ee){}

pack();

}

private JEditorPane createEditorPane() {

JEditorPane editorPane = new JEditorPane();

editorPane.setEditable(false);

java.net.URL helpURL = Documentacion.class.getResource("Tutorial.html");

if (helpURL != null) {

try {

editorPane.setPage(helpURL);

} catch (IOException e) {

System.err.println("Attempted to read a bad URL: " + helpURL);

}

} else {

System.err.println("Couldn't find file: TextSampleDemoHelp.html");

}

return editorPane;

}

String cadena;

int numero_filas;

private JLabel label1;

}

P.D. The app is a JInternalFrame you can change the app to JFrame.

ErnestoCarloa at 2007-7-10 14:07:21 > top of Java-index,Desktop,Core GUI APIs...
# 4

I know this thing

but mail content doesn't give me as such HTML page.so is there any other way to show.

Like I have one HTML page script as text and I want to show with it's format that how can we do that?

because it showing me the actual source code of HTML page but not in the format.

JTable_need_infoa at 2007-7-10 14:07:21 > top of Java-index,Desktop,Core GUI APIs...
# 5
You should set the context pane of the editor pane to html first:pane.setContentType("text/html");
Rodney_McKaya at 2007-7-10 14:07:21 > top of Java-index,Desktop,Core GUI APIs...