-
Enhancement
-
Resolution: Fixed
-
P4
-
1.3.1
-
None
-
1.4
-
sparc
-
solaris_7
When printing Java2D windows under UNIX using JDK 1.3.x,
the print dialog box is not very useable. You cannot change the
printer without invoking the PSPrinterJob.printDialog() method.
However, this dialog does not return any properties that can be
re-used for its next invocation. Likewise, you cannot print to a
file and have it remember the last destination file selection.
Cadence Design Systems Inc. has developed a simple workaround for the severe limitations of the Java2D print dialog box. The existing dialog box shipped with JDK 1.3.1 exhibits the following undesirable behavior:
1. Not being able to specify or change the default printer in
the dialog box and remember it for future invocations. Users
with long network printer names have difficulty using it.
2. Not being able to select a printer from a pull-down menu
list.
3. The print dialog box looks and feels completely different
from all the other dialog boxes in the Swing toolkit. The
print-to-file dialog has similar characteristics.
4. The dialog box always pops up in the upper right hand corner
of the window every time.
5. Many print attributes are missing altogether.
// Test class to demonstrate lack of features of PrinterJob.printDialog()
import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
// Compile with: javac TestPrint.java
// Run with: java TestPrint
public class TestPrint extends JPanel implements Printable, ActionListener {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static JButton button = new JButton("Print");
public TestPrint() {
setBackground(bg);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JFrame jb = new JFrame();
PrinterJob printJob = PrinterJob.getPrinterJob();
if (printJob != null) {
printJob.setPrintable(this);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
}
public void drawShapes(Graphics2D g2){
g2.setPaint(fg);
g2.setStroke(stroke);
g2.draw(new Rectangle2D.Double(10, 10, 210, 150));
}
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
drawShapes((Graphics2D) g);
return Printable.PAGE_EXISTS;
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new TestPrint());
f.setSize(250, 250);
f.show();
}
}
the print dialog box is not very useable. You cannot change the
printer without invoking the PSPrinterJob.printDialog() method.
However, this dialog does not return any properties that can be
re-used for its next invocation. Likewise, you cannot print to a
file and have it remember the last destination file selection.
Cadence Design Systems Inc. has developed a simple workaround for the severe limitations of the Java2D print dialog box. The existing dialog box shipped with JDK 1.3.1 exhibits the following undesirable behavior:
1. Not being able to specify or change the default printer in
the dialog box and remember it for future invocations. Users
with long network printer names have difficulty using it.
2. Not being able to select a printer from a pull-down menu
list.
3. The print dialog box looks and feels completely different
from all the other dialog boxes in the Swing toolkit. The
print-to-file dialog has similar characteristics.
4. The dialog box always pops up in the upper right hand corner
of the window every time.
5. Many print attributes are missing altogether.
// Test class to demonstrate lack of features of PrinterJob.printDialog()
import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
// Compile with: javac TestPrint.java
// Run with: java TestPrint
public class TestPrint extends JPanel implements Printable, ActionListener {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static JButton button = new JButton("Print");
public TestPrint() {
setBackground(bg);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JFrame jb = new JFrame();
PrinterJob printJob = PrinterJob.getPrinterJob();
if (printJob != null) {
printJob.setPrintable(this);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
}
public void drawShapes(Graphics2D g2){
g2.setPaint(fg);
g2.setStroke(stroke);
g2.draw(new Rectangle2D.Double(10, 10, 210, 150));
}
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
drawShapes((Graphics2D) g);
return Printable.PAGE_EXISTS;
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new TestPrint());
f.setSize(250, 250);
f.show();
}
}
- relates to
-
JDK-4285177 RFE: Additional Java 2D printing work
-
- Resolved
-
-
JDK-4137899 RFE: Get a list of printer devices
-
- Resolved
-