Results 1 to 2 of 2
Thread: Printing user's entry.
- 04-03-2010, 11:23 AM #1
Member
- Join Date
- Apr 2010
- Location
- Manchester
- Posts
- 5
- Rep Power
- 0
Printing user's entry.
Hi there guys. I have been trying to get the user's input from a textbox to print but so far have been unsuccessful. Can anyone point me in the right direction ?
Java Code:import java.awt.*; import java.awt.print.*; import javax.swing.*; import java.awt.event.*; public class PrintingTest extends javax.swing.JFrame implements Printable{ JPanel mainPanel = new JPanel(); JTextField txtTest = new JTextField("Write something here :D"); /** Creates new form PrintingTest */ public PrintingTest() { displayPanel(); } public void displayPanel() { JButton btnTest = new JButton("Test !"); mainPanel.add(txtTest); mainPanel.add(btnTest); btnTest.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnTestActionPerformed(evt); } }); this.add(mainPanel); mainPanel.setSize(400,200); mainPanel.setBackground(Color.YELLOW); } private void btnTestActionPerformed(java.awt.event.ActionEvent evt) { // Get the rep of the current printer & print job PrinterJob printerJob = PrinterJob.getPrinterJob(); // Page formating before printing PageFormat format = new PageFormat(); format = printerJob.pageDialog(format); // Build a book containing pair of page painters (Printables) // and PageFormats. This example has a single page. Book book = new Book(); book.append(new PrintingTest(), format); // Set the obj to be printed (book) into PrinterJob. printerJob.setPageable(book); // Show print dialog to user. boolean doPrint = printerJob.printDialog(); if (doPrint) { try { printerJob.print(); } catch (PrinterException e) { JOptionPane.showMessageDialog(null, "Unable to print + Error : " + e); } } } public int print(Graphics g, PageFormat format, int pageIndex) { String entry = txtTest.getText().trim(); // Java 2D Graphics2D g2d = (Graphics2D) g; // Move origin from the corner of paper to corner of imageable area g2d.translate(format.getImageableX(), format.getImageableY()); // Perform rendering of text g.drawString("What is going on ?" , 5, 10); g.drawString(entry , 5, 20); // Set text colour g2d.setPaint(Color.BLACK); return Printable.PAGE_EXISTS; } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PrintingTest().setVisible(true); } }); } }
- 04-03-2010, 04:00 PM #2
Member
- Join Date
- Apr 2010
- Location
- Manchester
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Web portal accessing files on the user's system via the Java I/O stream?
By rendy.dev in forum New To JavaReplies: 3Last Post: 01-14-2010, 04:22 AM -
Entry in table
By Harsh_Verma in forum New To JavaReplies: 2Last Post: 07-01-2009, 02:07 PM -
Why "Exception while reading user's input as an int"
By soc86 in forum New To JavaReplies: 1Last Post: 01-23-2009, 04:13 PM -
how applet is sent to user's machine
By amu in forum Java AppletsReplies: 1Last Post: 09-21-2008, 02:24 PM -
Viewing a user's periphials from an applet
By jason2li in forum Java AppletsReplies: 0Last Post: 08-07-2007, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks