Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print JEditorPane showing a HTML page

Status
Not open for further replies.

dirkm

IS-IT--Management
Aug 19, 2009
1
US
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:

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 =
	&quot;<html><head><title>Test</title></head><body>&quot;+
			&quot;<H1>Überschrift</H1> <HR>&quot;+
			&quot;<b>Label0: </b>Feld0 <BR>&quot;+
			&quot;<b>Label1: </b>Feld1 <BR>&quot;+
			&quot;blablabla...&quot;+
	&quot;</body></html>&quot;;

editorPane.setText(somehtml); 

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

try 
{
editorPane.setPage(url);
} catch (IOException e) {
System.err.println(&quot;Attempted to read a bad URL: &quot; + 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(&quot;out.html&quot;));
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 = &quot;file:&quot; + System.getProperty(&quot;user.dir&quot;) 
+ System.getProperty(&quot;file.separator&quot;) 
+ datei; 
url = new URL(s); 
} 
catch (Exception e) 
{ 
System.err.println(&quot;Couldn't create help URL: &quot; + s); 
}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top