Ive tried to do some printing of Swing components and didnt
succeed in anyway. I Think i know how to print normal
graphics outputted to Graphics class but outputting components
doesnt seem to work for me at all.

Here's the code ive tried:

<SNIP>

import java.lang.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
public class printTest extends JFrame implements Printable, ActionListener{
        private JPanel p;
        private JPanel p1;
        public printTest() {
                super("Print Test");
                JButton b = new JButton("Print");
                b.addActionListener(this);
                p1 = new JPanel();
                p = new JPanel();
                p.add(new JLabel("This is test label") );
                p.add(new JButton("BUTTON!"));
                this.getContentPane().setLayout( new GridLayout(2,1) );
                p1.setLayout( new GridLayout(2,1) );
                p1.setBackground(Color.black);
                p.setBackground(Color.white);
                p1.add(b);
                p1.add(p);
                this.getContentPane().add(p1);
        }
        public void actionPerformed(ActionEvent e) {
                PrinterJob pJob = PrinterJob.getPrinterJob();
                pJob.setPrintable(this);
                if (pJob.printDialog()) {
                        try {
                                pJob.print();
                        } catch(Exception ex){
                                ex.printStackTrace();
                        }
                }
        }
        public int print(Graphics graph, PageFormat pageFormat, int pageIndex) 
{
                if (pageIndex>=1) {
                        return Printable.NO_SUCH_PAGE;
                }
                p1.revalidate();
                p1.paint(graph);
                return Printable.PAGE_EXISTS;
        }
        public static void main(String args[]) {
                printTest app = new printTest();
                app.pack();
                app.setVisible(true);
        }
}
</SNIP>

The source *does* suck in many ways and it doesnt try to
be anything else but the test how to print the frigin component.

If this approach doesnt work at all, ill write the original
problem here to if anyone can point the way how to accomplish
this:

I have a set of HTML files and an application which has a listbox
where you can choose one of the files, then the application opens
one more window where the html file is being viewed. Now, 
user has capability to print this text out.

I Can print the html itself easily but it has to be translated/
stripped out of html tags before it can be printed so i thougt
i just print the swing component which can output htmlformatted
text correctly.




-- 
 --------------------------------------------------------- 
| Name     : Jani Mikkonen                                |
| Email    : [EMAIL PROTECTED]                              |  
| ICQ UIN# : 19840860                                     | 
| Homepage : http://www.pcuf.fi/~rasjani                  |
| ------------------------------------------------------- |
| 'Save the whales, Feed the hungry and Free the mallocs' |
 ---------------------------------------------------------


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to