我正在维护这个具有“打印”选项的 Swing 应用程序。需要防止用户以任何方式与底层文件系统进行交互,但打印对话框将“打印到文件”作为一台打印机提供,当然也允许从文件系统中选择目录和文件。
是否有一种轻松的方法来覆盖/修改打印对话框以从该对话框中隐藏“to file”打印机?我知道 API 可以让我零碎地完成这项工作,但我宁愿不必重新创建大部分对话框 GUI 和功能来执行此操作。
最佳答案
来自 Sun 论坛 (http://72.5.124.102/thread.jspa?messageID=10931926#10931926 ), 虽然你不能隐藏按钮,但看起来你可以捕获选择它的事件并抛出异常:
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService[] printerArray = dialogPrinters.toArray(new PrintService[dialogPrinters.size()]);
// call print dialog and get print attributes
PrintService selectedPrinter = javax.print.ServiceUI.printDialog(null, 200, 200, printerArray, defaultPrintService, flavor, pras);
// check if "print-to-file" option used
if (pras.get(Destination.class) != null)
{
// here we deny to perform the save into a file
JOptionPane.showMessageDialog(CMSJRViewer.this, getBundleString("error.printing"));
throw new PrintException("Print to file option not allowed. Action aborted!");
}
else
{
...
}
关于java - 在 Java 打印对话框中隐藏 "Print to file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2874374/