Java Print Dialog (native) - Change Icon

I use PrinterJob.printDialog() so that the user can select a printer and change various printing options.

However, the dialog is always displayed using the standard Java coffeecup icon, and not from my main window (JFrame).

How can I change the icon for this dialog?

I am using the following code snippet:

 PrinterJob pj = PrinterJob.getPrinterJob (); 
 pj.printDialog ();  // how do I change the icon for the dialog that is displayed here

 ... // process the selection from the dialog

Usually JDialog inherits the icon from the "parent" JFrame, but in this case I cannot pass or specify the parent window for this dialog

I am using java6

+8
java printing awt
source share
3 answers

It seems that a_horse_with_no_name is stuck (like everyone else) with the print dialog without a special icon. :-)

There will even be an iReport print dialog with a standard coffee cup icon. The print dialog does not behave like JFileChooser or JColorChooser. Fortunately, this is modal.

If the icon bothers you too much, you can create a wrapper class around it and work out the details as you like.

The Java6 API does not suggest changing the icon. I will live with a cup of coffee for a while and will wait for the next version of the JDK, which may offer behavior like JFileChooser.

+3
source share

I did not find a way to change the icon, but there is one indirect way to remove it.

You need to specify DialogOwner using print attributes. This causes java.awt.Window not to use the default Java icon.

 PrinterJob pj = PrinterJob.getPrinterJob(); // Create an Attribute set PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // A different way to bring Up Native Dialog from Java aset.add(sun.print.DialogTypeSelection.NATIVE); // Looks like this class is being moved to javax.print.attribute.standard for Java 7 // To Remove the Icon from the dialog provide an owner. Frame f = Frame(); aset.add(new sun.print.DialogOwner(f)); pj.printDialog(aset); // The dialog should not have an icon now. 

Hope this helps you!

While I keep looking for some way to post this print dialog. :)

+7
source share

I found a solution / workaround to change the Java print dialog box icon ( not ).

That is, this works for the print dialog presented 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 the delay and maxCount to suit your needs. Of course, there is always room for improvement.

Obviously, Timer must be started before any call to printDialog . For example, this also works if the timer starts before calling JTable.print() when showPrintDialog is true .

I am glad that I have a solution for an unanswered question for many years :) (at least in my project).

+1
source share

Source: https://habr.com/ru/post/650521/


All Articles