Java(本机)打印对话框 - 更改图标

发布于 2024-10-09 17:44:00 字数 423 浏览 0 评论 0原文

我使用 PrinterJob.printDialog() 让用户选择打印机并更改各种打印设置。

然而,该对话框始终使用标准 Java 咖啡杯图标显示,而不是我的主窗口 (JFrame) 中的图标。

如何更改该对话框的图标?

我正在使用以下代码:

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

通常,JDialog 会从“父”JFrame 继承图标,但在这种情况下,我无法传递或指定

我正在使用 Java6的该对话框的父窗口

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-10-16 17:44:00

我还没有找到更改图标的方法,但这是一种删除它的间接方法。

您需要通过打印属性指定 DialogOwner。这会导致 java.awt.Window 不使用默认的 Java 图标。

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.

希望这对你现在有帮助!

当我继续寻找某种方法来定位此打印对话框时。 :)

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

写给空气的情书 2024-10-16 17:44:00

看起来 a_horse_with_no_name 会被困在没有自定义图标的打印对话框中(就像我们其他人一样)。 :-)

甚至 iReport 的打印对话框也会显示标准的咖啡杯图标。打印对话框的行为与 JFileChooser 或 JColorChooser 不同。幸运的是它是模态的。

如果图标让您太烦恼,您可以围绕它创建一个包装类,并按照您喜欢的方式处理细节。

Java6 API 不提供修改图标的方法。我会忍受咖啡杯一段时间,并等待 JDK 的下一个版本可能会提供像 JFileChooser 这样的行为。

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.

时光磨忆 2024-10-16 17:44:00

我找到了一个解决方案/解决方法来更改 Java 打印对话框的图标(本机)。

也就是说,这适用于例如由 sun.print.ServiceDialog 表示的打印对话框。

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());

根据您的需要使用 delaymaxCount 值。当然,总是有改进的空间。

显然,Timer 必须在调用 printDialog 之前启动。例如,如果在 showPrintDialogtrue 时调用 JTable.print() 之前启动计时器,这也适用。

我很高兴我有一个多年来悬而未决的问题的解决方案:)(至少在我的项目中)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文