开发者

Java (native) print dialog - change icon

开发者 https://www.devze.com 2023-02-02 03:19 出处:网络
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

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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号