Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 26th, 2003, 10:20 AM
Registered User
 
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB Printer.print command

Is there a similar method in Java to Printer.print in VB or do you have to write your own.

And how would you print a text file to a printer.

Bill

 
Old December 27th, 2003, 01:59 AM
Authorized User
 
Join Date: May 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Following is the code for printing in java.

PrinterJob printJob = PrinterJob.getPrinterJob(); // Get a printing object
        printJob.setPrintable(theApp.getView()); // The view is the page source
    HashPrintRequestAttributeSet printAttr = new HashPrintRequestAttributeSet();
    if(printJob.printDialog(printAttr)) // Display print dialog
        { // If true is returned...
          try
          {
            printJob.print(printAttr); // then print
          }
          catch(PrinterException pe)
          {
            System.out.println(pe);
            JOptionPane.showMessageDialog(SketchFrame.this,
                                          "Error printing a sketch.",
                                          "Printer Error",
                                          JOptionPane.ERROR_MESSAGE);
          }
        }

Hope this helps

Regards


Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team
 
Old December 28th, 2003, 10:44 PM
Registered User
 
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Would please send me a simple example of printing a something.txt that I could compile. And an example of printing a variable via method..


 
Old December 30th, 2003, 09:08 AM
Authorized User
 
Join Date: May 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Following is the code for a sample application which has a simple menu and the content of the window is printed.

import javax.swing.JFrame;
import javax.swing.JEditorPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenuItem;
import java.awt.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet ;

public class PrintExample extends JFrame implements ActionListener,Printable
{
    private JEditorPane textarea;
    private JMenuBar mbar;
    private JMenu print;
    private JMenuItem mitem;
    String str = "This is some sample text which will be printed by this program as a test for printing";
    public PrintExample()
    {
        super("Simple Text Editor Example");
        setSize(600,600);
        this.getContentPane().setBackground(Color.white);
        init();
        show();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    private void init()
    {
        mbar = new JMenuBar();
        setJMenuBar(mbar);
        print = new JMenu("Print");
        mitem = new JMenuItem("Print Text");
        mitem.addActionListener(this);
        print.add(mitem);
        mbar.add(print);
        textarea = new JEditorPane();
        textarea.setContentType("text/plain");
        textarea.setText(str);
        getContentPane().add(textarea);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == mitem)
        {
            PrinterJob printJob = PrinterJob.getPrinterJob(); // Get a printing object
            printJob.setPrintable(this); // The view is the page source
            HashPrintRequestAttributeSet printAttr = new HashPrintRequestAttributeSet();
            if(printJob.printDialog(printAttr)) // Display print dialog
            { // If true is returned...
                 try
                  {
                    printJob.print(printAttr); // then print
                  }
                  catch(PrinterException pe)
                  {
                    System.out.println(pe);
                }
            }
        }

    }

    public int print(Graphics g, PageFormat pf, int index)
    {
        if(index>0)
            return NO_SUCH_PAGE;
        Graphics2D gr2 = (Graphics2D)g;
        gr2.translate(pf.getImageableX(),pf.getImageableY( ));
        paint(gr2);
        return Printable.PAGE_EXISTS;

    }

    public static void main(String[] args)
    {
        PrintExample px = new PrintExample();
    }
}

Regards and Happy New Year

Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team





Similar Threads
Thread Thread Starter Forum Replies Last Post
Print data report in dot matrix printer in VB p_rajaprabu Crystal Reports 1 April 20th, 2011 02:16 PM
how can i get vb printer.print code for a word doc choudhry.usman Word VBA 1 March 6th, 2008 10:46 PM
get the printer name chosen in print dialog deep_ocean Classic ASP Basics 0 March 14th, 2007 10:27 AM
Printer control language command Gret C# 0 February 2nd, 2006 01:14 AM
printer.print problem rajanikrishna Pro VB 6 5 December 13th, 2004 10:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.