Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 28 of 28

Thread: need help Printing output from a JTextfield

  1. #26
    Junior Member
    Join Date
    Jun 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help Printing output from a JTextfield

    nevermind, it printed.

  2. #27
    Junior Member
    Join Date
    Jun 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help Printing output from a JTextfield

    still cant figure out how to print. i dont understand java well enough to implement PrintUIWindow into my program.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: need help Printing output from a JTextfield

    The print() method in the PrintUIWindow program is called with a Graphics object that will contain what will be printed. In a GUI program the paintComponent() method is called with a Graphics object that will contain what will be displayed on the screen.

    The code that uses the Graphic object's draw...() methods will be the same code that is used to display the GUI and to print the GUI.

    In your program, write a drawing method that takes a Graphics object and draws what you want to see.
    Override the paintComponent() method for the container its to be shown in and in paintComponent() call your method with the Graphics object. Once you have the drawing method working, then use the code in PrintUIWindow to call YOUR drawing method with the Graphics object vs using the statement thats now in the program: frameToPrint.printAll(g)

    Here's a sample program:
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.print.*;
     
    public class ReportPrinter implements Printable, ActionListener {
     
        // The Printable interface
        public int print(Graphics g, PageFormat pf, int page) throws
                                                            PrinterException {
     
            if (page > 0) { /* We have only one page, and 'page' is zero-based */
                return NO_SUCH_PAGE;
            }
     
            /* User (0,0) is typically outside the imageable area, so we must
             * translate by the X and Y values in the PageFormat to avoid clipping
             */
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
     
            /* Now we perform our rendering */
            fillInPanel(g);   // Call our method to draw the panel
     
            /* tell the caller that this page is part of the printed document */
            return PAGE_EXISTS;
        }
     
        // -----------------------------------------------------
        public void actionPerformed(ActionEvent e) {
             PrinterJob job = PrinterJob.getPrinterJob();
             job.setPrintable(this);
             boolean ok = job.printDialog();
             if (ok) {
                 try {
                      job.print();
                 } catch (PrinterException ex) {
                  ex.printStackTrace(); /* The job did not successfully complete */
                 }
             }
        }
     
        // Build the report using the Graphics for the panel
        public static void fillInPanel(Graphics g) {
            g.drawString("Hello world!", 10, 20);
            g.drawString("Another message", 10, 40);
            g.drawString("More Stuff", 10, 60);
            g.drawString("And more", 10, 80);
        }
     
        // Start here - create frame and fill in panel
        public static void main(String args[]) {
     
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            JFrame f = new JFrame("Hello World Printer");
            f.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {System.exit(0);}
            });
            JButton printButton = new JButton("Print This");
            printButton.addActionListener(new ReportPrinter());
            f.add(printButton, BorderLayout.SOUTH);
            JPanel jp = new JPanel() {
               public void paintComponent(Graphics g) {
                  super.paintComponent(g);
                  fillInPanel(g);      // fill in panel
               }
            };
            f.add(jp,BorderLayout.CENTER);
            f.setSize(200, 200);
            f.setVisible(true);
        }
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  2. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  3. Issue with JTextField Locking
    By PekinSOFT.Systems in forum AWT / Java Swing
    Replies: 0
    Last Post: October 1st, 2009, 11:12 AM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM