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:dummy@domain.com?cc=test@domain.com&
            // subject=First%20Email&&body=Hello%20World
            //
            String message = "mailto:dummy@domain.com?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);
    }
}