How can I get/set the paper tray of printers? My intentions are to allow people to select a printer from inside a java-applet & let them set the printer and the papertray and store those in a server-sided database.
Then when they want to print something I want to programmaticaly set the bin/printer and print on that one.

So what I need (in order of importance):
- How to get/set paper format (-)
- How to get/set the printer name (+)
- How to get/set the paper tray (++)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

See here:
There are some example to print settings.
About the insert into DB you can use a normal JDBC (I believe you know how to use it).

Bye, Giant.
First, you will need to use the Properties class to set the properties you want. In order to determine the correct values, that will depend on the printer.

But, perhaps more importantly, this does not work on most Java releases.
half_life_foolAuthor Commented:
Is it possible to give some examples? Because I am totally puzzled here. I am rather good at C++, but this Java thing eludes me :(
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

You would start the prinjob with:

Properties prop = new Properties();
Toolkit tk = getToolkit();
PrintJob pj = tk.getPrintJob(frame, "job title", prop);

This will work in all JRE releases (Microsoft, Sun, ...);

For Sun JRE 1.3 and later, you can use:

JobAttributes ja = new JobAttributes();
ja.setPrinter("printer name");
PageAttributes pa = new PageAttributes();
PrintJob = tk.setPrintJob(frame, "job title", ja, pa);

I did not find a way to set the tray using these, and how well they work still depends on the printer in the user's system, and whether or not it supports these functions. I have not used the second form mysefl, because the needs of my applications have required suport for 1.1, and have not had need for the level of support granted by the newer method.

For more informatio on these, see:
half_life_foolAuthor Commented:
       JobAttributes theJobAttribs = new JobAttributes();
        theJobAttribs.setDialog( JobAttributes.DialogType.NONE );
        PageAttributes thePageAttribs = new PageAttributes();
        thePageAttribs.setMedia( PageAttributes.MediaType.LETTER );
//        thePageAttribs.setMedia( MediaTray.MAIN );
        PrintJob myJob = getToolkit().getPrintJob(this, "PrintJob", theJobAttribs, thePageAttribs);
        if (myJob != null) {
            Graphics g = myJob.getGraphics();
            if (g != null) {
                printText(myJob, g, "yeqsd");

This however gives an error on getToolkit() & on printText()

Also there SHOULD be a possibility to get/set the MediaTray.

What errors are generated by these calls?

getToolkit() is defined in java.awt.Component, so it shuold be callable from within any class that extends that.

I have not used the javax.print classes, but yes, it does look like you could use DocPrintJob to add the additional support you mention. The only caveat is that I am not sure with JRE that was implemented in, or if that is a concern for you.
half_life_foolAuthor Commented:
D:\Documents and Settings\Steven\JavaApplication2\src\javaapplication2\ cannot find symbol
symbol  : method getPrintJob(javaapplication2.Main,java.lang.String,java.awt.JobAttributes,java.awt.PageAttributes)
location: class java.awt.Toolkit
        PrintJob myJob = getToolkit().getPrintJob(this, "PrintJob", theJobAttribs, thePageAttribs);
D:\Documents and Settings\Steven\JavaApplication2\src\javaapplication2\ cannot find symbol
symbol  : method printText(java.awt.PrintJob,java.awt.Graphics,java.lang.String)
location: class javaapplication2.Main
                printText(myJob, g, "yeqsd");
2 errors
What version of Java are you running this under? The revised getPrintJob method was first made available with Java 1.3. If you are using an earlier release, you need to stick with my original recommendation. In that case, I would recommend that you start the print job, using the Print Dialgo to set the options that you want, and then, when getPrintJob() returns, dump the contents of the Properties object to the Java console, so you can preload those settings.

As for the printText method, I cannot speak to that, as I do not know that method. Is that one you wrote? If not, what class is it from?
half_life_foolAuthor Commented:
I'm running it under java 1.5.0.

I am coding in Netbeans 4.0 beta.

BTW, how do I dump that object to the Java console? Because when I set a watch in NB, it doesn't provide me with much usefull information...

A simple way to output the properies object would be as follows:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I looked at the code you said was reporting errors. When I brought it up in Eclispe with the JDK 1.5 libraries, I got an error that setAttribute() was not a member of PrintJob, and could not find printText, which, as I said, looks like one of your methods.
I do not know if the author ever found a solution, though I tried to direct him as much as I could based on the information provided. My recommendation would be to mark my comment from 10/31/2004 05:26AM CST as accepted.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.