Java(本机)打印对话框 – 更改图标
我使用PrinterJob.printDialog()
让用户选择打印机并更改各种打印设置。
但是,对话框始终使用标准Java coffeecup图标显示,而不是主窗口(JFrame)中的图标。
如何更改该对话框的图标?
我正在使用以下代码:
PrinterJob pj = PrinterJob.getPrinterJob(); pj.printDialog(); //如何更改此处显示的对话框的图标 ... //从对话框中处理选择
通常,JDialog从“父”JFrameinheritance图标,但在这种情况下,我无法传递或指定该对话框的父窗口
我正在使用Java6
似乎a_horse_with_no_name将被卡住(就像我们其他人一样),打印对话框没有自定义图标。 🙂
甚至iReport的打印对话框也会显示标准的咖啡杯图标。 “打印”对话框的行为与JFileChooser或JColorChooser不同。 幸运的是它是模态的。
如果图标困扰你太多,你可以围绕它创建一个包装类,并按照你喜欢的方式计算出细节。
Java6 API无法修改图标。 我将与咖啡杯一起生活一段时间,并将等待可能提供JFileChooser等行为的下一版JDK。
我还没有找到更改图标的方法,但这是一种间接的方法来删除它。
您需要通过print属性指定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.
希望这对你有所帮助!!
我继续寻找一些方法来定位这个打印对话框。 🙂
我找到了一个解决方案/解决方法来更改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());
根据您的需要播放delay
和maxCount
值。 当然,总有改进的余地。
显然,必须在调用printDialog
之前启动printDialog
。 例如,如果在showPrintDialog
为true
时调用JTable.print()
之前启动计时器,这也有效。
我很高兴多年来我找到了一个未解决问题的解决方案:)(至少在我的项目中)。