Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
PageSetup and Printing of JTextArea contents

843807
Member Posts: 46,582
Dear All,
I am implementing a small text editor program, using
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.
Regards,
Sudheendran T L
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
-
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); } }); } }
-
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.
-
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 -
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 -
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.