Hi,
I have a program which use JDIC webbrowser to display an html page.
the webbrowser.print() has been called to print the page. The page is
displayed properly on the window. But the printing does nopt happen.
No error messages also. Any help will be greatly appreaciated.
plz find the source of my program below:
public HTMLViewer(URL url) {
this.url=url;
WebBrowser webBrowser = new WebBrowser();
webBrowser.setSize(1000,700);
JFrame frame = new JFrame("My Report");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar;
JButton print=new JButton("Print");
menuBar = new JMenuBar();
menuBar.add(print);
frame.setJMenuBar(menuBar);
Container contentPane = frame.getContentPane();
contentPane.add(webBrowser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
webBrowser.setURL(url);
PrintButtonListener p= new PrintButtonListener(webBrowser);
print.addMouseListener(p);
}
//main method:
public static void main(args[]){
HTMLViewer h= new HTMLViewer();
}
//inner class:
class PrintButtonListener extends MouseAdapter{
WebBrowser webBrowser=new WebBrowser();
public PrintButtonListener(WebBrowser webBrowser){
this.webBrowser=webBrowser;
}
public void mouseClicked(java.awt.event.MouseEvent evt) {
webBrowser.print(webBrowser.getGraphics());
}
}
PM2K - 06 Jan 2008 21:01 GMT
Read JavaDoc 4 webBrowser.print carefully:
----------------------------------------------
Prints this component. Applications should override this method for
components that must do special processing before being printed or
should be printed differently than they are painted.
The default implementation of this method calls the paint method.
The origin of the graphics context, its (0, 0) coordinate point, is
the top-left corner of this component. The clipping region of the
graphics context is the bounding rectangle of this component.
See Also:
paint(Graphics)
-----------------------------------------------
The most important is this sentence:
The default implementation of this method calls the paint method.
This method isn't 4 printing to printer. Try another way, 4 example:
class PrintButtonListener extends MouseAdapter {
WebBrowser webBrowser=new WebBrowser();
public PrintButtonListener(WebBrowser webBrowser){
this.webBrowser=webBrowser;
}
public void mouseClicked(java.awt.event.MouseEvent evt) {
webBrowser.executeScript("window.print();");
}
}
Enjoy!