Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:46:58+00:00 2026-05-26T01:46:58+00:00

We are using the new Java printing API which uses PrinterJob.printDialog(attributes) to display the

  • 0

We are using the new Java printing API which uses PrinterJob.printDialog(attributes) to display the dialog to the user.

Wanting to save the user’s settings for the next time, I wanted to do this:

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

    saveAttributesToPreferences(attributes);
}

However, what I found by doing this is that sometimes (I haven’t figured out how, yet) the attributes get some bad data inside, and then when you print, you get a white page of nothing. Then the code saves the poisoned settings into the preferences, and all subsequent print runs get poisoned settings too. Additionally, the entire point of the exercise, making the settings for the new run the same as the user chose for the previous run, is defeated, because the new dialog does not appear to use the old settings.

So I would like to know if there is a proper way to do this. Surely Sun didn’t intend that users have to select the printer, page size, orientation and margin settings every time the application starts up.

Edit to show the implementation of the storage methods:

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());
}

Edit: Okay, it seems like the reason it isn’t remembering the printer is that it isn’t in the PrintRequestAttributeSet at all. Indeed, the margins and page sizes are remembered, at least until the settings get poisoned at random. But the printer chosen by the user is not here:

[0] = {java.util.HashMap$Entry@9494} class javax.print.attribute.standard.Media -> na-letter
[1] = {java.util.HashMap$Entry@9501} class javax.print.attribute.standard.Copies -> 1
[2] = {java.util.HashMap$Entry@9510} class javax.print.attribute.standard.MediaPrintableArea -> (10.0,10.0)->(195.9,259.4)mm
[3] = {java.util.HashMap$Entry@9519} class javax.print.attribute.standard.OrientationRequested -> portrait
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:46:59+00:00Added an answer on May 26, 2026 at 1:46 am

    It appears that what you’re looking for is the PrintServiceAttributeSet, rather than the PrintRequestAttributeSet.

    Take a look at the PrintServiceAttribute interface, and see if the elements you need have been implemented as classes. If not, you can implement your own PrintServiceAttribute class(es).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have client which has a problem with printing from JAVA applications, using old
I'm relatively new to Java and I'm using a new API. I came across
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
For a new Java web project I thought about using: Eclipse as IDE Mercurial
Using the new version of the substance look and feel for Java, the connecting
I am new to Java and am trying to run a program using Eclipse.
I am new to Java world but I am pretty good at using Flex,
Is it possible to do a C like fork in java, using an new
I'm new to using Hibernate with Java. I'm getting the following exception. The stuff
I'm new to unit testing using nunit (and Java development in general). When creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.