Displaying Print Dialog

Hi,

I would like to display a print dialog (prss Print button from JPanel).  I use the following code:

import java.awt.print.*;

...

PrinterJob job = PrinterJob.getPrinterJob();



The code compiles, but I get this (on the line above) error:

java.lang.NoClassDefFoundError: java/awt/print/PrinterJob
    at com.ignitus.SwitchInventory.SwitchInventorySelect.help_actionPerformed_Interaction1(SwitchInventorySelect.java:124)
    at com.ignitus.SwitchInventory.SwitchInventorySelect.help_actionPerformed(SwitchInventorySelect.java:116)
    at com.ignitus.SwitchInventory.SwitchInventorySelect$SymAction.actionPerformed(SwitchInventorySelect.java:107)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1066)
    at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1101)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:217)
    at java.awt.Component.processMouseEvent(Component.java:2355)
    at java.awt.Component.processEvent(Component.java:2200)
    at java.awt.Container.processEvent(Container.java:904)
    at java.awt.Component.dispatchEventImpl(Component.java:1809)
    at java.awt.Container.dispatchEventImpl(Container.java:949)
    at java.awt.Component.dispatchEvent(Component.java:1741)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:1739)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:1537)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:1453)
    at java.awt.Container.dispatchEventImpl(Container.java:936)
    at java.awt.Window.dispatchEventImpl(Window.java:491)
    at java.awt.Component.dispatchEvent(Component.java:1741)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:81)


Thanks,

Alex.
akoifmanAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
expertmbConnect With a Mentor Commented:
hi,
try this

 void print_actionPerformed(ActionEvent e) {
       PrintJob pj =
                  f.getToolkit().getPrintJob(f, "print1", null);
  }

mb...
0
 
zhongbingCommented:
It's strange, make sure you jdk is correctly installed and the CLASSPATH is correctly configured. Maybe your rt.jar was broken.
0
 
zhongbingCommented:
maybe the best way is reinstall your JDK.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
akoifmanAuthor Commented:
Hi,

What is F in
PrintJob pj =
      f.getToolkit().getPrintJob(f, "print1", null);
 

How do I display the print dialog?

Thanks,

Alex.
0
 
expertmbCommented:
>>What is F in
Frame f = new Frame();

mb...
0
 
akoifmanAuthor Commented:
Hi,

I need PrinterJob to display the Printer dialog box.  PrintJob does not do it:

Error: C:\nms\com\ignitus\SwitchInventory\Report.java(266): Method printDialog() not found in java.awt.PrintJob

This is the code I am trying to use:

PrinterJob printJob = PrinterJob.getPrinterJob();
            //printJob.setPrintable(this);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
0
 
expertmbCommented:
0
 
heyhey_Commented:
what's your OS / JDK version / Swing version ?
0
 
vladi21Commented:
My answer:

Java 1.2
7.2.3.2 Print dialog

Typically, an application presents a print dialog to the user when a
print menu item or button is activated. To display this print dialog,
you call @PrinterJob.printDialog@ method. The user's choices in the
dialog are constrained based on the number and format of the pages in
the @Printable@ or @Pageable@ that have been furnished to the
@PrinterJob@.  If the user clicks OK in the print dialog, @printDialog@
returns @true.@  If the user cancels the print dialog, @false@ is
returned, and the print job should be considered abandoned.

---
So if u use Java 1.2 u can find answer here
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/render.html

also look
http://manning.spindoczine.com/sbe/files/uts2/Chapter22html/Chapter22.htm
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html


0
All Courses

From novice to tech pro — start learning today.