java - 在 Java 打印对话框中隐藏 "Print to file"

标签 java security user-interface swing printing

我正在维护这个具有“打印”选项的 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/

相关文章:

java - shutdownHooks RuntimePermission 的作用是什么?

security - Docker 中的 Volume 是一个安全漏洞吗?

java - Web如何防止修改元素属性值引起的安全问题?如禁用或隐藏

java - Vaadin 是否像 Vaadin Table UI 组件中的列一样使用为 Table.addItem() 提供的 itemId?

android - 有没有办法让 UI 元素稍微重叠(一个在另一个之上)而不使用绝对布局?

java - 如何在文本字段中获取日期

java - JFrame 和 JPanel 居中

java - 这个 add() 函数如何工作,为什么返回 null?

java - NodeConnectorRef 的 java 文件在哪里?

java - Android 中的 slider 控件... SeekBar 真的是我最好/唯一的选择吗?