Java Printer Usage getDefaultPrinterName()

Here you can find the source of getDefaultPrinterName()

Description

get Default Printer Name

License

GNU General Public License

Declaration

public static String getDefaultPrinterName() 

Method Source Code

//package com.java2s;
/*/*  ww  w  .ja v a2  s .co  m*/
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import javax.print.*;

public class Main {
    private static String defaultPrinterName = null;
    private static boolean bWaitForPrinterServer = false;
    private static long lastQueryTime = 0;

    public static String getDefaultPrinterName() {
        if (bWaitForPrinterServer)
            return defaultPrinterName;
        bWaitForPrinterServer = true;
        if (defaultPrinterName == null) {
            long t = System.currentTimeMillis() / 1000;
            if ((t - lastQueryTime) < 120) {
                bWaitForPrinterServer = false;
                return defaultPrinterName;
            }
            PrintService ps = PrintServiceLookup
                    .lookupDefaultPrintService();
            lastQueryTime = System.currentTimeMillis() / 1000;
            String name = null;
            if (ps != null) {
                name = ps.getName();
                if (name != null && name.length() > 0)
                    defaultPrinterName = name;
            }
        }
        bWaitForPrinterServer = false;
        return defaultPrinterName;
    }
}

Related

  1. findPrintService(String printerName)
  2. get_print_service(String printerName, HashPrintRequestAttributeSet aset)
  3. getArrayOfPrinterNames()
  4. getAttribute( PrintService ps, Class category)
  5. getAvailablePrinters()
  6. getDefaultPrinterName()
  7. getDefaultPrintRequestAttributes()
  8. getEnumAttribute(String name, Object value)
  9. getIntegerAttribute(String name, int value)