Printing works on windows but not on linux

U

Ulrich Eckhardt

Hi,

i have written a small testprogramm for printing. This testprogramm
should print out the halve page in green. On windows this programm
works as expected but on linux (OpenSuse 10.1 JDK 1.4.2-12 and 1.5.0-7)
i get only a blank page. Even if i redirect the printer output into
a file and view it with ghostscript the page is empty. Has
anybody an idea whats going wrong ?

public class testFrame extends JFrame implements Printable {

private JButton jButton = null;
private PrinterJob printJob = null;
/**
* This method initializes
*
*/
public testFrame() {
super();
initialize();
}

/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(new Dimension(173, 123));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Test");
this.setVisible(true);
this.setContentPane(getJButton());
printJob = PrinterJob.getPrinterJob();
//Book bk = new Book();
//bk.append(this, printJob.defaultPage());

// Pass the book to the PrinterJob
// printJob.setPageable(bk);
printJob.setPrintable(this);
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Print");
jButton.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent e) {
System.out.println ("Print");
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
dispose();
}
}
});
}
return jButton;
}

/**
* @param args
*/
public static void main(String[] args) {
testFrame mW = new testFrame();
System.out.println("Starting");
mW.initialize ();
}

public int print (Graphics g, PageFormat pf, int pi) throws
PrinterException {

if (pi > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.green);
g2.setBackground(Color.gray);
g2.scale (1/72,1/27);
g2.fillRect ((int)pf.getImageableX(),
(int)pf.getImageableY(),
(int)pf.getImageableWidth(),
(int)pf.getImageableHeight() / 2);

return Printable.PAGE_EXISTS;
}
}

Thanks for any help
Uli
 
D

danharrisandrews

Hi Ulrich,

Try the following test. First add a close button. Remove the
"dispose()" line from your print button's action and move it to a new
action listener that you add to the close button. Off the top of my
head, my guess is that your window is closed and or the VM exits before
the printing is complete.

I think that the most elegant way to do this sort of printing is to use
the a javax.print.DocPrintJob and add a PrintJobListener and on
notification of printJobCompleted, printJobFailed, printJobCanceled, or
printJobNoMoreEvents then it should be OK to close your window.

For more information on using the DocPrintJob have a look at the Java
Print Service API (http://java.sun.com/j2se/1.4.2/pdf/JPS_PDF.pdf).

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -

Ulrich said:
Hi,

i have written a small testprogramm for printing. This testprogramm
should print out the halve page in green. On windows this programm
works as expected but on linux (OpenSuse 10.1 JDK 1.4.2-12 and 1.5.0-7)
i get only a blank page. Even if i redirect the printer output into
a file and view it with ghostscript the page is empty. Has
anybody an idea whats going wrong ?

public class testFrame extends JFrame implements Printable {

private JButton jButton = null;
private PrinterJob printJob = null;
/**
* This method initializes
*
*/
public testFrame() {
super();
initialize();
}

/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(new Dimension(173, 123));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Test");
this.setVisible(true);
this.setContentPane(getJButton());
printJob = PrinterJob.getPrinterJob();
//Book bk = new Book();
//bk.append(this, printJob.defaultPage());

// Pass the book to the PrinterJob
// printJob.setPageable(bk);
printJob.setPrintable(this);
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Print");
jButton.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent e) {
System.out.println ("Print");
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
dispose();
}
}
});
}
return jButton;
}

/**
* @param args
*/
public static void main(String[] args) {
testFrame mW = new testFrame();
System.out.println("Starting");
mW.initialize ();
}

public int print (Graphics g, PageFormat pf, int pi) throws
PrinterException {

if (pi > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.green);
g2.setBackground(Color.gray);
g2.scale (1/72,1/27);
g2.fillRect ((int)pf.getImageableX(),
(int)pf.getImageableY(),
(int)pf.getImageableWidth(),
(int)pf.getImageableHeight() / 2);

return Printable.PAGE_EXISTS;
}
}

Thanks for any help
Uli
--
Ulrich Eckhardt http://www.uli-eckhardt.de

Ein Blitzableiter auf dem Kirchturm ist das denkbar stärkste
Misstrauensvotum gegen den lieben Gott. (Karl Krauss)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top