Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

HTML in JTextPane?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you insert HTML into a JTextPane? I am trying to just append HTML to the end of the text pane. I tried
text.setText(text.getText() + "
new text");
but I got an EmptyStackException.
I also found an insertHTML method in HTMLEditorKit, but it has loads of arguments that I don't know the purpose of. I just want to append HTML to the textpane!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can give you an example with JEditorPane.
You will have an html file which will be displayed in the Pane.
Take care that the class has to implement HiperlinkListener interface, and also Runnable
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
import java.applet.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Help extends JFrame implements WindowListener,HyperlinkListener, Runnable
{
JEditorPane htmlArea=null;
JScrollPane scrollPane;
URL url;
Cursor cursor=null;

public Help()
{
super("Help");
this.setIconImage((new ImageIcon("middle.gif")).getImage());
this.getContentPane().setLayout(new BorderLayout());

try{
htmlArea = new JEditorPane();
url=new URL("file:///e:/An4/Posta/help.html");
htmlArea.setEditorKit(htmlArea.getEditorKitForContentType("html"));
htmlArea.setPage(url);
htmlArea.setEditable(false);
htmlArea.addHyperlinkListener(this);
}
catch( Exception e4){System.out.println(e4.getMessage());};

scrollPane = new JScrollPane(htmlArea);
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
this.addWindowListener(this);
this.pack();
this.setSize(500,400);this.setResizable(false);
this.show();
}

public void hyperlinkUpdate(HyperlinkEvent e)
{
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
linkActivated(e.getURL());
}
}

protected void linkActivated(URL u)
{
Cursor c = htmlArea.getCursor();
Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
htmlArea.setCursor(waitCursor);
this.url = u;
this.cursor = c;
SwingUtilities.invokeLater(this);
}

public void run()
{
if (url == null)
{
htmlArea.setCursor(cursor);
Container parent = htmlArea.getParent();
parent.repaint();
}
else
{
Document doc = htmlArea.getDocument();
try
{
htmlArea.setPage(url);
}
catch (IOException ioe){
htmlArea.setDocument(doc);
getToolkit().beep();
}
finally
{
url = null;
SwingUtilities.invokeLater(this);
}
}
}
} // class Help
 
Gary Bryan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but what I was trying to do was to append HTML to a textpane (or editor pane) which already has HTML in it. Thanks for the example anyway, it might come in useful sometime.
 
Ana Mihailescu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as I know, an HTML file has a well defined structure, with
<html>, <head>, <body> , etc tags...
If you append somenthig at the end, you destroy this structure,
isn't it? Maybe you want to append in the body of the html text,
but anyway , I don't know how...
Ana
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Gary,
The following code uses the same method that you told, but does not raise any exception.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TPane extends JFrame implements ActionListener
{
JTextPane tp=new JTextPane();
JButton jb=new JButton("PressMe");

public TPane(){
Container cp=getContentPane();
cp.add(tp,BorderLayout.CENTER);
cp.add(jb,BorderLayout.SOUTH);
jb.addActionListener(this);
setSize(600,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tp.setText(tp.getText() + " new Text.");

}
public static void main(String[] args)
{
TPane jtp=new TPane();
}
}

Originally posted by Gary Bryan:
How do you insert HTML into a JTextPane? I am trying to just append HTML to the end of the text pane. I tried
text.setText(text.getText() + "
new text");
but I got an EmptyStackException.
I also found an insertHTML method in HTMLEditorKit, but it has loads of arguments that I don't know the purpose of. I just want to append HTML to the textpane!


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic