2010-12-29 37 views
8

私はPrinterJob.printDialog()を使用して、ユーザーがプリンタを選択してさまざまな印刷設定を変更できるようにします。Java(ネイティブ)印刷ダイアログ - アイコンを変更

ただし、ダイアログは常にメインのウィンドウ(JFrame)のものではなく、標準Java coffeecupアイコンを使用して表示されます。

このダイアログのアイコンを変更するにはどうすればよいですか?

私は次のコードを使用しています:

 
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からアイコンを継承しますが、この場合には、私はそのダイアログの親ウィンドウを渡すか指定することはできません

私はa_horse_with_no_nameが無いカスタムアイコンと印刷ダイアログで(私たちの残りの部分のように)立ち往生するだろうと思われJava6の

答えて

3

を使用しています。 :-)

iReportの印刷ダイアログでも、標準のコーヒーカップアイコンが表示されます。印刷ダイアログは、JFileChooserやJColorChooserのようには動作しません。幸いにもそれはモーダルです。

アイコンがあまりにも煩わしい場合は、その周りにラッパークラスを作成し、好きなように細部を整えることができます。

Java6 APIではアイコンを変更できません。私はコーヒーカップでしばらくの間暮らし、JFileChooserのような動作を提供する次のバージョンのJDKを待つことになります。

+1

ありがとうございました!私が望む答えではありませんが、少なくとも私は周りを見回すことはできません。 –

7

アイコンを変更する方法が見つかりませんでしたが、削除する間接的な方法が1つあります。

印刷属性で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. 

これはあなたのために今役立ちます!

私はこの印刷ダイアログの位置を決める方法を探し続けていますが、 :)

+0

ありがとう、アイコンは実際にこの方法で削除されます。 –

+0

- アクセス制限:必要なライブラリの制限のため、コンストラクタDialogOwner(Frame)にアクセスできません/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/ \t rt.jar – AvrDragon

+0

私はそれを見つけることができなかったので、Java 7のためのこのコード。 DialogTypeSelectionで参照が見つかりましたが、うまくいきませんでした。 –

1

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値と遊びます。もちろん、常に改善の余地があります。

printDialogを呼び出す前に、Timerを開始する必要があります。たとえば、showPrintDialogtrueのときにJTable.print()を呼び出す前にタイマーを起動すると、これも機能します。

私は何年もの間、未回答の質問に対する解決策を持っていることを嬉しく思っています:)(少なくとも私のプロジェクトでは)。

関連する問題