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 there could be the Windows Paint or GIMP on the Linux operating system.

package org.kodejava.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.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.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. An UnsupportedOperationException will
        // be thrown if the platform doesn't support Desktop API.
        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.
            String message = "mailto:[email protected][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.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("https://kodejava.org");
        try {
            // Get Desktop instance of the current browser context. An 
            // UnsupportedOperationException will be thrown if the 
            // platform doesn't support Desktop API. 
            Desktop desktop = Desktop.getDesktop();

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

How do I get and display an image in Applet?

To display an image in an applet we first need to obtain the image object itself. A call to applet’s getImage(URL url, String name) method help us to create an image object from the specified URL of the image.

For displaying the image on the applet’s screen we draw it in the paint() method using Graphics.drawImage() method.

package org.kodejava.applet;

import java.applet.Applet;
import java.awt.*;

public class AppletGetImage extends Applet {
    private Image logo;

    @Override
    public void init() {
        // Get an Image object that can be painted on the Applet
        // screen. We need to supply the URL of the document as
        // the base location of the image and the location of the
        // image relative the base URL. 
        logo = getImage(getDocumentBase(), "/images/logo.png");
    }

    @Override
    public void paint(Graphics g) {
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        g.drawImage(logo, 10, 10, this);
    }
}
** Deprecated: The Applet API is deprecated since JDK 9, no replacement.