Results 1 to 2 of 2
  1. #1
    mConfused is offline Member
    Join Date
    Apr 2010
    Location
    Manchester
    Posts
    5
    Rep Power
    0

    Default 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);
                }
            });
        }
    }

  2. #2
    mConfused is offline Member
    Join Date
    Apr 2010
    Location
    Manchester
    Posts
    5
    Rep Power
    0

    Default

    Hi guys. I manage to find the solution but a very basic one. It is as simple as just getting the textfield/textarea and add a print method. But now the difficult question is to get multiple inputs and print it all into one page. if anyone has an experience, do post it here ! Thanks :)

Similar Threads

  1. Replies: 3
    Last Post: 01-14-2010, 05:22 AM
  2. Entry in table
    By Harsh_Verma in forum New To Java
    Replies: 2
    Last Post: 07-01-2009, 03:07 PM
  3. Replies: 1
    Last Post: 01-23-2009, 05:13 PM
  4. how applet is sent to user's machine
    By amu in forum Java Applets
    Replies: 1
    Last Post: 09-21-2008, 03:24 PM
  5. Viewing a user's periphials from an applet
    By jason2li in forum Java Applets
    Replies: 0
    Last Post: 08-07-2007, 03:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •