Daron
Daron

Reputation: 329

contents of textarea to the printer

I am able to easily print the contents of a JTable mainly through using the statement below. However, this does not work for printing the contents of a textarea component. Is there a simple way of printing the contents of a textarea component? I've seen some pretty confusing examples on the web that use multiple classes. But I'm trying to find a simpler way of doing this.

I ADDED THE SECOND AREA OF CODE ABOUT 24 HOURS AFTER THE ORIGINAL POST. NOTE THAT IT SEEMS TO BE PROMISING BUT PRODUCES THE ERROR "ADD ARGUMENT TO MATCH PRINT(GRAPHICS)". HOW CAN THAT CODE BE FIXED?

table.print(JTable.PrintMode.NORMAL, header, footer);




JButton btnNewButton_7 = new JButton("Print");
    btnNewButton_7.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent arg0) {  

        try{
            boolean complete = textArea_2.print();
            //The above line reads the error "Add argument to match print(Graphics)"
            if(complete){
                JOptionPane.showMessageDialog(null,  "Printjob Finished", "Report",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null, "Printing", "Printer", JOptionPane.ERROR_MESSAGE);
                }
            }catch(PrinterException e){JOptionPane.showMessageDialog(null, e);
            }



        }

    });

Upvotes: 0

Views: 2039

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Google and the JavaDocs are your friend

JTextArea#print

A convenience print method that displays a print dialog, and then prints this JTextComponent in interactive mode with no header or footer text. Note: this method blocks until printing is done. Note: In headless mode, no dialogs will be shown.

This method calls the full featured print method to perform printing.

Returns:
true, unless printing is canceled by the user

JTextArea#print(MessageFormat headerFormat, MessageFormat footerFormat)

A convenience print method that displays a print dialog, and then prints this JTextComponent in interactive mode with the specified header and footer text. Note: this method blocks until printing is done. Note: In headless mode, no dialogs will be shown.

This method calls the full featured print method to perform printing.

Parameters:
headerFormat - the text, in MessageFormat, to be used as the header, or null for no header
footerFormat - the text, in MessageFormat, to be used as the footer, or null for no footer
Returns: true, unless printing is canceled by the user

JTextArea#print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive) throws PrinterException

...You can look that one yourself, it's to long to post...

JTextArea#getPrintable(MessageFormat headerFormat, MessageFormat footerFormat)

Returns a Printable to use for printing the content of this JTextComponent. The returned Printable prints the document as it looks on the screen except being reformatted to fit the paper. The returned Printable can be wrapped inside another Printable in order to create complex reports and documents. The returned Printable shares the document with this JTextComponent. It is the responsibility of the developer to ensure that the document is not mutated while this Printable is used. Printing behavior is undefined when the document is mutated during printing.

Page header and footer text can be added to the output by providing MessageFormat arguments. The printing code requests Strings from the formats, providing a single item which may be included in the formatted string: an Integer representing the current page number.

The returned Printable when printed, formats the document content appropriately for the page size. For correct line wrapping the imageable width of all pages must be the same. See PageFormat.getImageableWidth().

This method is thread-safe, although most Swing methods are not. Please see How to Use Threads for more information.

The returned Printable can be printed on any thread.

This implementation returned Printable performs all painting on the Event Dispatch Thread, regardless of what thread it is used on.

Parameters:
headerFormat - the text, in MessageFormat, to be used as the header, or null for no header
footerFormat - the text, in MessageFormat, to be used as the footer, or null for no footer
Returns:
a Printable for use in printing content of this JTextComponent

Works just fine for me...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPrint {

    public static void main(String[] args) {
        new TestPrint();
    }

    public TestPrint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JTextArea ta = new JTextArea(20, 100);
                try (FileReader reader = new FileReader(new File("C:/Script.txt"))) {
                    ta.read(reader, ta);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                JButton print = new JButton("Print");
                print.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        MessageFormat header = new MessageFormat("Star Wars IV A new Hope");
                        MessageFormat footer = new MessageFormat("Page {0}");
                        try {
                            ta.print(header, footer);
                        } catch (PrinterException ex) {
                            ex.printStackTrace();
                        }
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(ta));
                frame.add(print, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 1

Related Questions