• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Printing HTML out of a JEditorPane

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Is there an easy way to print a html document, that is displayed in a JEditorPane using the HTMLEditorKit. The document contains multiple pages. But there are no graphics, just formatted Text (bold, different fontsize,...) and horizontal lines.
Thanks a lot,
Dirk
Here comes the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.print.*;

public class HTML extends JFrame implements ActionListener, Printable
{
private URL url;
private JMenuBar menu = new JMenuBar();
private JMenu datei = new JMenu("Datei");
private JMenuItem drucken = new JMenuItem("Drucken");
private JMenuItem speichern = new JMenuItem("Speichern");
private JMenuItem schliessen = new JMenuItem("Schliessen");
private JEditorPane editorPane = new JEditorPane();

public HTML()
{
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});

menu.add(datei);
datei.add(drucken);
datei.add(speichern);
datei.add(schliessen);

setJMenuBar(menu);

drucken.addActionListener(this);
speichern.addActionListener(this);
schliessen.addActionListener(this);


editorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(editorPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

getContentPane().add(scrollPane);

setSize(600,700);
setLocation(50,50);

editorPane.setContentType("text/html");
editorPane.setEditorKit(new HTMLEditorKit());

String somehtml =
"<html><head><title>Test</title></head><body>"+
"<H1>�berschrift</H1>
"+
"Label0: Feld0
"+
"Label1: Feld1
"+
"blablabla..."+
"</body></html>";

editorPane.setText(somehtml);

/* //Initialisierung aus Datei
seturl("test.html");

try
{
editorPane.setPage(url);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}

*/




}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==schliessen)
{
dispose();
System.exit(0);
}
if (e.getSource()==speichern)
{
createFile();
}
if (e.getSource()==drucken)
{
//IS THIS THE RIGHT WAY, OR IS THERE ANY EASIER WAY???
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try{ pj.print(); }
catch (Exception PrintE) {System.err.println(PrintE.toString());}

}
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
{
//???
return NO_SUCH_PAGE;
}


public void createFile()
{
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter("out.html"));
bw.write(editorPane.getText());
bw.close();

}
catch(IOException IOEx)
{
System.err.println(IOEx.toString());
}
}

public static void main(String args[])
{
HTML f = new HTML();
f.setVisible(true);
}

public void seturl(String datei)
{
String s = null;
try
{
s = "file:" + System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ datei;
url = new URL(s);
}
catch (Exception e)
{
System.err.println("Couldn't create help URL: " + s);
}
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic