Click to See Complete Forum and Search --> : Displaying contents of HTML page in Table Format in JEditorpane and printing it?


K srikanth
August 21st, 1999, 09:01 AM
This below code is not working and not printing.Please send codes and suggestions to srikanthkailas@yahoo.com
<javacode>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;

class MyEditorPane extends JEditorPane implements Printable{

public MyEditorPane() throws IOException {
super("jEditorPane1");
this.setContentType("text/html");
this.setPage("file:///C:/TEMP/b.html");


}

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {

Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(), pf.getImageableY());
Font f = new Font("Monospaced",Font.PLAIN,12);
g2.setFont(f);
paint(g2);
// getContentPane().paint(g2);
return Printable.PAGE_EXISTS;
}
}


public class PrintFrame extends JFrame {

JScrollPane jScrollPane1 = new JScrollPane();
JEditorPane jEditorPane1;
JButton generate,print;



public PrintFrame() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
jScrollPane1 = new JScrollPane();
generate = new JButton("Generate");
print = new JButton("Print");

jScrollPane1.getViewport().setLayout(null);
this.getContentPane().setLayout(null);
jScrollPane1.setPreferredSize(new Dimension(20, 20));
jScrollPane1.setBounds(new Rectangle(26, 12, 313, 220));
jEditorPane1 = new MyEditorPane();
// jEditorPane1.setContentType("text/html");

jEditorPane1.setBounds(new Rectangle(4, 6, 308, 211));
// jEditorPane1.setPage("file:///C:/TEMP/b.html");



generate.setBounds(new Rectangle(31, 253, 71, 23));
generate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
generate_actionPerformed(e);
}
});

print.setActionCommand("print");

print.setBounds(new Rectangle(100, 253, 71, 23));
print.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
print_actionPerformed(e);
}

});
this.getContentPane().add(jScrollPane1, null);
jScrollPane1.getViewport().add(jEditorPane1, null);
this.getContentPane().add(generate, null);
this.getContentPane().add(print, null);
// this.setContentPane(jEditorPane1);

}



void print_actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("print")) {
PrinterJob pjob = PrinterJob.getPrinterJob();
// pjob.setPrintable((Printable) jEditorPane1);
pjob.setPrintable((Printable)this);
PageFormat pf = pjob.pageDialog(pjob.defaultPage());
if (pjob.printDialog()) {
try{
pjob.print();

} catch (Exception ex) { ex.printStackTrace(); }
}
}
}
void generate_actionPerformed(ActionEvent e) {


}


public static void main(String args[]) {

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
};
/* Frame test = new Frame("printframe");*/
PrintFrame test = new PrintFrame();
test.addWindowListener(l);

test.pack();
test.setSize(new Dimension(500,400));
test.show();
}
}


</javacode>