In Java how do I change or set a default printer

I know how to get the list of available printers, I want users to be able to select from a list and set that to the default for the session

Using Windows 7

I know that this is easily done I just want to create a simple java program a: To increase my knowledge b: Teachers here are very adverse to playing with printing properties

Thanks for your help in advance


This program works in Eclipse.

import java.awt.print.PageFormat;

import java.awt.print.PrinterJob;

public class PrinterSetup 
{

    public static void main(String[] args) throws Exception
    {
        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat pf = pjob.defaultPage();
        pjob.setPrintable(null, pf);

        if (pjob.printDialog()) {
          pjob.print();
        }
    }
}

In Java how do I change or set a default printer, Notice that the orientation setting is never changed. 5. Close JPS print dialog. 6. Click "2D" button. 7. Do the following several times: * change a  This PowerShell script shows how to set default printer in Windows. Script How to set default printer using PowerShell This site uses cookies for analytics, personalized content and ads.


You know how to get list of all printers then you want set a default printer.

ok this code will help you can Pass Name of printer which you want to set as default printer where "MYPRINTER" .replace it with name of printer.
PrinterJob pj = PrinterJob.getPrinterJob();
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of printers configured: " + printServices.length);
    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        if (printer.getName().equals("***MYPRINTER***")) {
            try {
                pj.setPrintService(printer);
            } catch (PrinterException ex) {
            }
        }
    }

Mechanism to set default printer settings in a , JDK-4744405 - RFE: lookupPrintServices() to refresh the printers list dynamically 2 Runtime Environment, Standard Edition (build 1.4.2-b28) Java HotSpot(TM) Client The "Internal default printer" will change to the new default you just set. To modify these settings for all documents you print, from the Control Panel choose View Devices and Printers (in the Hardware and Sound group). In the resulting Devices and Printers window, any printers you have installed are listed. Right-click a printer and then choose Printing Preferences. In the Printing Preferences dialog box that appears, click any of the tabs to display various settings, such as Color or Paper.


I made a workaround to set the OS default printer. This one works for windows which basically executes a cmd command that sets the default printer before executing the print code:

Desktop desktop = null
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop()
    desktop.print(file)
 }

Here's my function:

public static boolean setDefaultPrinter(String printerName) {
    String defaultPrinterSetter = "wmic printer where name='"+ printerName +"' call setdefaultprinter";
    try {
        setDefaultPrinterProcess = Runtime.getRuntime().exec("cmd /c start cmd.exe /C " + defaultPrinterSetter);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

All you need to do is pass the printer name to this function and it'll make it the default printer.

Here's a function that gets a list of all avaiable printer services:

public static ArrayList<PrintService> availablePrinters() {
    PrintService[] services = PrinterJob.lookupPrintServices();
    ArrayList<PrintService> allServices = new ArrayList<>();

    for (PrintService myService : services) {
        allServices.add(myService);
    }
    return allServices;
}

And I'll assume you'd want to add a list in maybe a combobox or something for user to choose from. It should go something like this

    ArrayList<PrintService> availableServices = availablePrinters();
    System.out.println("All printers list:");
    for (PrintService myService : availableServices) {
        myCombo.getItems().add(myService.getName());
        System.out.println(myService.getName());
    }

JDK-6182986 Default printer lookup doesn't track changes to the , Your print job will automatically send to the default printer destination unless otherwise specificied. Identify the Default Printer. In the lower-left corner of your  To set a default printer on your personal computer (PC): On your desktop, click Start > Settings > Printers and Faxes . All the printers and faxes you have access to will display. Highlight the printer you want to set as the default. On the Toolbar, click File.


Printer: Set or Change the Default Printer - Help Pages, This policy setting allows you to specify whether the client default printer is automatically set as the default printer in a session on an RD Session Host server​. In the Printers & scannerswindow, click on the printer you want to set as the default printer and click the Managebutton. Under Manage your device, click the Set as defaultbutton. The printer should have the word "Default" below it on the main Printers & scannerswindow.


Do not set default client printer to be default printer in a session, This setting applies to all users in that company. Set General User Currency. User currency settings determine the currency used as the default for what you see  It will be reset to your default printer when you restart the application. So you'll have to do a "Ctrl+P" to bring up the Print dialog, change the printer to the one you want & press close. Now the Quick Print button will print to that printer.


Setup and Configuration (Chapter 2) R19D, To print documents from Tray 2, you must change the print setting to Tray 2. Paper does not automatically pull from Tray 2 when your product is set to the auto​  All you need to do is pass the printer name to this function and it'll make it the default printer. Here's a function that gets a list of all avaiable printer services: public static ArrayList<PrintService> availablePrinters() { PrintService[] services = PrinterJob.lookupPrintServices(); ArrayList<PrintService> allServices = new ArrayList<>(); for (PrintService myService : services) { allServices.add(myService); } return allServices;}