Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
edit() - Launch Default Editor for Given File
This section provides a tutorial example on how to use Desktop.edit() method to launch the default editor for the given file based on the file type.
If you have a file and want to launch its associated default editor on the current desktop of the operating system, you can get the default Desktop object and call its edit() method. Here is a sample program:
/* DesktopEditFile.java * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.File; public class DesktopEditFile { public static void main(String[] a) { try { File file = new File("Swing.jpg"); Desktop desktop = Desktop.getDesktop(); if (desktop != null) desktop.edit(file); } catch (Exception e) { e.printStackTrace(); } } }
Make sure you have image file called Swing.jpg in the working directory. Then run this program, you will see the default image editor program is started with the given image file. On my Windows 10 computer, I am getting the "Paint" app launched.
On my macOS computer, I am getting the Preview app launched.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JCheckBox - Swing Check Box Class
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
JComboBox - Swing Combo Box Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)
►Integration with Desktop System
javax.awt.Desktop and Related Methods
browse() - Launch Default Web Browser
mail() - Launch Default Email Program