How do I print a file using the default registered application?

The example code below shows you how to print a file using the default registered application’s print command for the corresponding file type. As an example, on Windows notepad.exe is the default application for printing a .txt file.

To print a file using the default registered application we call the java.awt.Desktop.print(File) method. The print() method takes a parameter of File, which is the reference to a file to be printed. The code snippet below, when run on Windows, will open notepad.exe and print the data.txt file.

package org.kodejava.example.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppPrint {
    public static void main(String[] args) {
        File file = new File("data.txt");
        try {
            Desktop desktop = Desktop.getDesktop();

            //
            // Prints a file with the native desktop printing
            // facility, using the associated application's
            // print command.
            //
            desktop.print(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I edit a file using the default registered application?

For editing a file using the default registered or associated application we can do a call to the java.awt.Desktop.edit(File) method. In the code snippet below we will edit a PNG file. Using the edit() method of the Desktop class will open the default registered application for PNG files. For example on Windows the could be the Windows Paint or GIMP on the Linux operating system.

package org.kodejava.example.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppEdit {
    public static void main(String[] args) {
        File file = new File("logo.png");
        try {
            //
            // Edit a file using the default program for the
            // file type. In the example we will launch a
            // default registered program to edit a PNG image.
            //
            Desktop desktop = Desktop.getDesktop();
            desktop.edit(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I open a file using the default registered application?

If you want to open a file using the default registered or associated application for those files you can use the Desktop.open(File file) method call. In the example below we’ll ask the Desktop class to open a text file.

package org.kodejava.example.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppOpen {
    public static void main(String[] args) {
        //
        // A reference to a text file
        //
        File file = new File("data.txt");

        try {
            Desktop desktop = Desktop.getDesktop();

            //
            // Open a file using the default program for the
            // file type. In the example we will launch a
            // default registered program to open a text file.
            // For example on Windows operating system this call
            // might launch a notepad.exe to open the file.
            //
            desktop.open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I launch user-default mail client application?

Here is an example using the java.awt.Desktop class to open user’s default mail client application. There are two methods provided, the mail() and the mail(URI uri) methods.

When specifying the URI to the application will be opened with the message field filled with the mailto information. You can refer to the following document for the valid mailto URI scheme http://www.ietf.org/rfc/rfc2368.txt.

package org.kodejava.example.awt;

import java.awt.*;
import java.io.IOException;
import java.net.URI;

public class RunningDefaultMailClient {
    public static void main(String[] args) {
        //
        // Get an instance of Desktop. If the platform doesn't
        // support Desktop API an UnsupportedOperationException
        // will be thrown.
        //
        Desktop desktop = Desktop.getDesktop();

        try {
            //
            // Open user-default mail client application.
            //
            desktop.mail();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            //
            // Open user-default mail client with the email message
            // fields information.
            //
            // mailto:[email protected][email protected]&
            // subject=First%20Email&&body=Hello%20World
            //
            String message = "mailto:[email protected]?subject=First%20Email";
            URI uri = URI.create(message);
            desktop.mail(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I launch user-default web browser?

The code below show you how to browse a website using the user’s default web browser. To get the default web browser you can use the Desktop class browse(URI uri) method call.

package org.kodejava.example.awt;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;

public class RunningDefaultBrowser {
    public static void main(String[] args) {
        URI uri = URI.create("http://www.kodejava.org");
        try {
            //
            // Get Desktop instance of the current browser context.
            // If the platform doesn't support Desktop API an
            // UnsupportedOperationException will be thrown.
            //
            Desktop desktop = Desktop.getDesktop();

            //
            // Browse the uri using user's default web browser.
            //
            desktop.browse(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I turn the Scroll Lock button on?

The program below show you how to turn the scroll lock button on programatically. Setting the locking state to Boolean.TRUE activate the scroll lock.

package org.kodejava.example.awt;

import java.awt.*;
import java.awt.event.KeyEvent;

public class TurnScrollLockOn {
    public static void main(String[] args) {
        //
        // Gets the default toolkit.
        //
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        
        //
        // Update the locking state for scroll lock button to true
        // will turn the scroll lock on.
        //
        toolkit.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, Boolean.TRUE);
    }
}

How do I turn the Num Lock button on?

The example below show you how to activate the num lock button programmatically. Setting the locking state to Boolean.TRUE will turn the num lock on.

package org.kodejava.example.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class TurnNumLockOn {
    public static void main(String[] args) {
        //
        // Gets the default toolkit.
        //
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        //
        // Update the locking state for num lock button to true
        // will turn the num lock on.
        //
        toolkit.setLockingKeyState(KeyEvent.VK_NUM_LOCK, Boolean.TRUE);
    }
}

How do I turn the Caps Lock button on?

The code below show you how to activate the caps lock button programmatically. Setting the locking state to Boolean.TRUE will turn the caps lock on.

package org.kodejava.example.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class TurnCapsLockOn {
    public static void main(String[] args) throws Exception {
        //
        // Gets the default toolkit.
        //
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        //
        // Update the locking state for caps lock button to true 
        // will turn the caps lock on.
        //
        toolkit.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, Boolean.TRUE);
    }
}

How do I read the status of Scroll Lock key?

This example show you how to detect if the Scroll Lock key is in active mode.

package org.kodejava.example.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class ScrollLockState {
    public static void main(String[] args) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        //
        // Get the locking state of the Scroll Lock button. If it is "on"
        // this method return boolean true value.
        //
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);

        System.out.println("ScrollLock button is " + (isOn ? "on" : "off"));
    }
}

If you run the program you will get an output telling you that the scroll lock button is active or not active.

How do I read the status of Num Lock key?

This example show you how to detect if the Num Lock key is in active mode.

package org.kodejava.example.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class NumLockState {
    public static void main(String[] args) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        //
        // Get the locking state of the Num Lock button. If it is "on"
        // this method return boolean true value.
        //
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK);

        System.out.println("NumLock button is " + (isOn ? "on" : "off"));
    }
}

If you run the program you will get an output telling you that the num lock button is active or not active.