0

I have code that should print content of my JTextPane control, but nothing prints on page. Page is blank. Here is my code:

 @Override
        public void actionPerformed(ActionEvent arg0) {
            // kod za printanje sadrzaja iz JTextPane-a
            /*
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(new Editor());
            boolean ok = job.printDialog();
            if(ok){
                try{
                    job.print();
                }
                catch(PrinterException pex){
                    JOptionPane.showMessageDialog(new Editor(), "Greška pri printanju dokumenta!", "Poruka", JOptionPane.INFORMATION_MESSAGE);
                }
                */
            try{
                //System.out.println(tekst1.getText());
                // PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
                  //  attr_set.add(MediaSizeName.ISO_A4);
                tekst1.setContentType("text/html");


                tekst1.print();
            }
            catch(Exception pex){
                pex.printStackTrace();
            }


        }
    };

Could anyone help me!?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nedimo
  • 307
  • 3
  • 9
  • 21
  • 1
    Is there actually anything loaded in the text pane? – MadProgrammer Mar 15 '14 at 20:24
  • More info on the tekst1 object would be nice. Is that you're text pane? If so I think you're calling `print` incorrectly, look at the [**documentation**](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#print(java.awt.Graphics)) – tanishalfelven Mar 15 '14 at 21:19
  • yes, object tekst1 is my JTextPane – Nedimo Mar 16 '14 at 11:00

1 Answers1

1

As you have defined content type to text/html hence try it after setting HTML editor kit.

jTextPane.setEditorKit(new HTMLEditorKit());

or you can try it without any editor kit by setting content typt to text/pain

jTextPane.setContentType("text/plain");

or removing content type.

//jTextPane.setContentType("text/html");

For more information see Java doc of method JEditorPane.setContentType()


A sample code with screenshots:

Note: save printed file as Microsoft XPS Document Writer

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLEditorKit;

public class PrintJTextPane {
    public static void main(String[] args) {
        JFrame jframe = new JFrame();
        jframe.setSize(500, 200);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTextPane jTextPane = new JTextPane();

        jTextPane.setEditorKit(new HTMLEditorKit());

        JButton btn = new JButton("Print");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    jTextPane.setContentType("text/html");

                    boolean done = jTextPane.print();
                    if (done) {
                        JOptionPane.showMessageDialog(null, "Printing is done");
                    } else {
                        JOptionPane.showMessageDialog(null, "Error while printing");
                    }
                } catch (Exception pex) {
                    JOptionPane.showMessageDialog(null, "Error while printing");
                    pex.printStackTrace();
                }
            }
        });

        jframe.add(btn, BorderLayout.SOUTH);

        jframe.add(jTextPane);
        jframe.setVisible(true);
    }
}

C1

C2

C3

Braj
  • 46,415
  • 5
  • 60
  • 76
  • This does not work for me, I want to send content of the JTextPane to a local printer. – Nedimo Mar 16 '14 at 16:57
  • Are you able to write it as an `xps` file? – Braj Mar 16 '14 at 18:33
  • May be there is some issue with printer setting. Please read [Simple page size setting when printing JTextPane?](http://stackoverflow.com/questions/16427874/simple-page-size-setting-when-printing-jtextpane) – Braj Mar 16 '14 at 18:36
  • I am able to write it as `Microsoft OneNote 2010` file. Right now I don't have any printer connected to my system. I will test it in my office and will confirm you back. – Braj Mar 16 '14 at 19:15