2010-11-24 6 views
13

PrinterJob.printDialog(attributes)を使用する新しいJava印刷APIを使用して、ダイアログをユーザーに表示しています。次回のために、ユーザーの設定を保存したくてスイングアプリでプリンタの設定を維持する良い方法はありますか?

が、私はこれをやってみたかった:

PrintRequestAttributeSet attributes = loadAttributesFromPreferences(); 
if (printJob.printDialog(attributes)) { 
    // print, and then... 

    saveAttributesToPreferences(attributes); 
} 

しかし、私がこれを行うことによって発見することは時々(私はまだ、どのように考え出したていない)ということです属性が内部に不正なデータを取得し、印刷すると何もない白いページが表示されます。次に、コードは毒のある設定を設定に保存し、その後のすべての印刷の実行も中毒の設定になります。さらに、新しいダイアログに以前の設定が使用されていないため、新しいランの設定を以前のランのために選択したものと同じにして、練習の全体が敗北します。

これを行う適切な方法があるかどうかを知りたいと思います。確かにSunは、アプリケーションを起動するたびに、ユーザーがプリンタ、ページサイズ、印刷方向、および余白の設定を選択する必要はないと考えていました。

編集は、ストレージ・メソッドの実装を表示します

private PrintRequestAttributeSet loadAttributesFromPreferences() 
{ 
    PrintRequestAttributeSet attributes = null; 

    byte[] marshaledAttributes = preferences.getByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, null); 
    if (marshaledAttributes != null) 
    { 
     try 
     { 
      @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) 
      ObjectInput objectInput = new ObjectInputStream(new ByteArrayInputStream(marshaledAttributes)); 

      attributes = (PrintRequestAttributeSet) objectInput.readObject(); 
     } 
     catch (IOException e) 
     { 
      // Can occur due to invalid object data e.g. InvalidClassException, StreamCorruptedException 
      Logger.getLogger(getClass()).warn("Error trying to read print attributes from preferences", e); 
     } 
     catch (ClassNotFoundException e) 
     { 
      Logger.getLogger(getClass()).warn("Class not found trying to read print attributes from preferences", e); 
     } 
    } 

    if (attributes == null) 
    { 
     attributes = new HashPrintRequestAttributeSet(); 
    } 

    return attributes; 
} 

private void saveAttributesToPreferences(PrintRequestAttributeSet attributes) 
{ 
    ByteArrayOutputStream storage = new ByteArrayOutputStream(); 
    try 
    { 
     ObjectOutput objectOutput = new ObjectOutputStream(storage); 
     try 
     { 
      objectOutput.writeObject(attributes); 
     } 
     finally 
     { 
      objectOutput.close(); // side-effect of flushing the underlying stream 
     } 
    } 
    catch (IOException e) 
    { 
     throw new IllegalStateException("I/O error writing to a stream going to a byte array", e); 
    } 

    preferences.putByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, storage.toByteArray()); 
} 

編集:さて、それはプリンタを覚えていません理由のように思えるそれはPrintRequestAttributeSetではないということですまったく。確かに、マージンとページサイズは少なくとも設定が無作為に中毒になるまで記憶されます。しかし、ユーザーが選択したプリンタはここではない:何を探していることはむしろPrintRequestAttributeSetより、PrintServiceAttributeSetであることが表示されます

[0] = {[email protected]} class javax.print.attribute.standard.Media -> na-letter 
[1] = {[email protected]} class javax.print.attribute.standard.Copies -> 1 
[2] = {[email protected]} class javax.print.attribute.standard.MediaPrintableArea -> (10.0,10.0)->(195.9,259.4)mm 
[3] = {[email protected]} class javax.print.attribute.standard.OrientationRequested -> portrait 
+0

'saveAttributesToPreferences()'の正しい動作を確認しましたか? – trashgod

+0

「正しい」を定義してください。それをバイト配列にシリアライズし、それをプリファレンスに格納しますが、それが正しいかどうかはこの質問に対する答えに依存します。 – Trejkaz

答えて

1

PrintServiceAttributeインターフェイスを見て、必要な要素がクラスとして実装されているかどうかを確認してください。そうでない場合は、独自のPrintServiceAttributeクラスを実装できます。

+0

うん...それは一つだった。その属性セットで終わるもののいくつかを削除するために少し慎重にする必要はありませんが、両方のセットを直列化するときに機能します。 – Trejkaz

+0

私はあなたがこれらの属性をどのように得ることができるかを見ていますが、別のPrinterJobでどのように復元していますか? –

+0

@Maurice Perry:Trejkazには、彼の質問にloadAttributesFromPreferences()メソッドがあります。プリファレンスまたはリソースファイルに属性を保存し、後でそれらの属性を読み込みます。 –

関連する問題