This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

PageSetup and Printing of JTextArea contents

843807
843807 Member Posts: 46,582
Dear All,

I am implementing a small text editor program, using
JTextArea
. The program has PageSetup and Print menu items.

The PageSetup menu item handler is given below and is working. Meaning if I do a PageSetup and change the page options like Landscape to portrait etc, the changes are checked to be retained when I enter PageSetup again, as pageFormat retains them.
// instance variable
PageFormat pageFormat = printerJob.defaultPage();

// handler code
void filePageSetupHandler( ActionEvent ae ) {
	pageFormat=printerJob.pageDialog(pageFormat);
}
However, how do I use the pageFormat obtained in the above handler to implement the Print handler? I have tried various things, but no suceess. Here is the partially completed Print handler code.
void filePrintHandler( ActionEvent ae ) {
	// how do I use the pageFormat information obtained earlier, in this place???
	textArea.print(null,null,true,null,null,true);
}
Any help will be greatly appreciated.

Regards,
Sudheendran T L

Comments

  • 800346
    800346 Member Posts: 1,094
    Try using a PrintRequestAttributeSet. Example:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.print.*;
    import java.text.MessageFormat;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import javax.swing.*;
    
    public class PrintDemo extends JFrame {
    
        private JButton btPrint;
        private JCheckBox cbPortrait;
        private JToolBar toolbar;
        private JTextArea textarea;
        static private PrintRequestAttributeSet attr;
    
        public PrintDemo() {
            super("PrintDemo");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(600, 300);
            setLocationRelativeTo(null);
            textarea = new JTextArea();
            toolbar = new JToolBar();
            btPrint = new JButton("Print");
            cbPortrait = new JCheckBox("Portrait");
            textarea.setText("print(textarea.getPrintable(new MessageFormat(" +
                    "\"PrintDemo\"), null), cbPortrait.isSelected());");
            getContentPane().add(new JScrollPane(textarea), BorderLayout.CENTER);
            toolbar.add(btPrint);
            cbPortrait.setSelected(true);
            toolbar.add(cbPortrait);
            getContentPane().add(toolbar, BorderLayout.PAGE_START);
            btPrint.addActionListener(new ActionListener() {
    
                public void actionPerformed(final ActionEvent evt) {
                    print(textarea.getPrintable(new MessageFormat("PrintDemo"), null), cbPortrait.isSelected());
                }
            });
            cbPortrait.addActionListener(new ActionListener() {
    
                public void actionPerformed(final ActionEvent evt) {
                    attr = null;
                }
            });
        }
    
        static public void print(final Printable printable) {
            print(printable, true);
        }
    
        static public void print(final Printable printable, final boolean portrait) {
            print(printable, portrait, new Insets(10, 10, 10, 10));
        }
    
        static public void print(final Printable printable, final boolean portrait, final Insets insets) {
            PrinterJob pjob = PrinterJob.getPrinterJob();
            pjob.setPrintable(printable);
            // create an attribute set to store attributes from the print dialog
            if (attr == null) {
                attr = new HashPrintRequestAttributeSet();
                float leftMargin = insets.left;
                float rightMargin = insets.right;
                float topMargin = insets.top;
                float bottomMargin = insets.bottom;
                if (portrait) {
                    attr.add(OrientationRequested.PORTRAIT);
                } else {
                    attr.add(OrientationRequested.LANDSCAPE);
                    leftMargin = insets.top;
                    rightMargin = insets.bottom;
                    topMargin = insets.right;
                    bottomMargin = insets.left;
                }
                attr.add(MediaSizeName.ISO_A4);
                MediaSize mediaSize = MediaSize.ISO.A4;
                float mediaWidth = mediaSize.getX(Size2DSyntax.MM);
                float mediaHeight = mediaSize.getY(Size2DSyntax.MM);
                attr.add(new MediaPrintableArea(
                        leftMargin, topMargin,
                        (mediaWidth - leftMargin - rightMargin),
                        (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));
            }
    //        if (pjob.printDialog(attr)) {
                try {
                    pjob.print(attr);
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
    //        }
        }
    
        public static void main(final String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new PrintDemo().setVisible(true);
                }
            });
        }
    }
  • darrylburke
    darrylburke Member Posts: 18,007
    Note: This thread was originally posted in the [New To Java|http://forums.sun.com/forum.jspa?forumID=54] forum, but moved to this forum for closer topic alignment.
  • 843807
    843807 Member Posts: 46,582
    Dear Mr. Andre Uhres,

    Thanks for your response.

    Though I was expecting a much shorter solution, I will check based on your inputs and keep you all posted of the outcome.

    Regards,
    Sudheendran T L
  • 843807
    843807 Member Posts: 46,582
    Dear Andre,
    Dear All,

    I am trying to use the print method of the JTextComponent class. (Ref [http://java.sun.com/docs/books/tutorial/uiswing/misc/printtext.html|http://java.sun.com/docs/books/tutorial/uiswing/misc/printtext.html]
    ).

    I want to use the following method especially:
    boolean complete = textComponent.print(MessageFormat headerFormat,
              		               MessageFormat footerFormat, 
                                                   boolean showPrintDialog,
                                                   PrintService service
                                                   PrintRequestAttributeSet attributes,
                                                   boolean interactive);
    I have already saved the pageFormat using the pageDialog method of PrinterJob. My question is how do I translate ALL the content of the pageFormat into the PrintRequestAttributeSet? For example, I have used the orientation from the PageFormat saved to set the apporpriate attribute. See below code. How do I set other attributes like the Paper size and margins?

    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    if ( pageFormat.getOrientation() == PageFormat.LANDSCAPE ){
        pras.add(OrientationRequested.LANDSCAPE);
    } else {
        pras.add(OrientationRequested.PORTRAIT);
    }
    Will appreciate any help.

    Regards,
    Sudheendran T L
  • 800346
    800346 Member Posts: 1,094
    The sample code that I posted above looks a bit complicated only because the page setup is done programmatically. If you want to display the standard page dialog (which might however look complicated to the user), you simply pass the "PrintRequestAttributeSet" that you also use for printing:
    pjob.pageDialog(attr);
    textComponent.print(null, null, true, pjob.getPrintService(), attr, true);
This discussion has been closed.