Java (native) print dialog - change icon
Asked Answered
C

3

9

I use PrinterJob.printDialog() to let the user select a printer and change various print settings.

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

How can I change the icon for that dialog?

I'm using the following piece of code:

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

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

I'm using Java6

Cowles answered 29/12, 2010 at 13:23 Comment(0)
R
3

It seems that a_horse_with_no_name will be stuck (like the rest of us) with a print dialog with no custom icon. :-)

Even iReport's print dialog appears with the standard coffee-cup icon. Print dialog does not behave like JFileChooser or JColorChooser. Fortunately it is modal.

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

Java6 API offers no way of modifying the icon. I will live with the coffee-cup for a while and will wait for the next version of the JDK that may offer a behaviour like JFileChooser.

Rosariarosario answered 29/12, 2010 at 19:17 Comment(0)
L
8

I have not found a way to change the icon, but here is one indirect way to Remove it.

You need to specify a DialogOwner via the print attributes. This causes java.awt.Window to not 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 for now!!

While I continue to search for some way to position this print dialog. :)

Lateral answered 16/3, 2011 at 20:15 Comment(4)
- Access restriction: The constructor DialogOwner(Frame) is not accessible due to restriction on required library /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/ rt.jarZuckerman
What is the code for this for Java 7 since I could not find it. I found a reference in DialogTypeSelection but that didn't work...Coverall
Well, I was looking for another issue but you saved my day thanks to your Native Dialog different way to bring up.Macmahon
Read this answer if you are looking at this - https://mcmap.net/q/1314892/-how-to-remove-java-icon-in-print-dialog-on-java-7-duplicateAloin
R
3

It seems that a_horse_with_no_name will be stuck (like the rest of us) with a print dialog with no custom icon. :-)

Even iReport's print dialog appears with the standard coffee-cup icon. Print dialog does not behave like JFileChooser or JColorChooser. Fortunately it is modal.

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

Java6 API offers no way of modifying the icon. I will live with the coffee-cup for a while and will wait for the next version of the JDK that may offer a behaviour like JFileChooser.

Rosariarosario answered 29/12, 2010 at 19:17 Comment(0)
K
2

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).

Kapor answered 15/5, 2016 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.