I've found a solution/workaround to change the icon of a Java print dialog (not native).
That is, this works for a print dialog represented by sun.print.ServiceDialog
, for example.
public static void changePrintDialogIcon(final Image icon) {
int delay = 10;
final int maxCount = 100;
final Container callerRoot = FocusManager.getCurrentManager().getCurrentFocusCycleRoot();
final Timer timer = new Timer(delay, null);
timer.addActionListener(new ActionListener() {
private int n = 0;
@Override
public void actionPerformed(ActionEvent e) {
Container currentRoot = FocusManager.getCurrentManager().getCurrentFocusCycleRoot();
if (callerRoot != currentRoot && currentRoot instanceof JDialog) {
JDialog serviceDialog = (JDialog) currentRoot;
serviceDialog.setIconImage(icon);
timer.stop();
} else if (n >= maxCount)
timer.stop();
}
});
timer.start();
}
Image icon = ...;
changePrintDialogIcon(icon);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.printDialog(new HashPrintRequestAttributeSet());
Play with delay
and maxCount
values according to your needs. Of course, there is always room for improvement.
Obviously, the Timer
must be started before any call to printDialog
. For instance, this also works if the timer is started before calling JTable.print()
when showPrintDialog
is true
.
I'm glad I have a solution for an unanswered question for years :) (at least in my project).