Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
browse() - Launch Default Web Browser
This section provides a tutorial example on how to use Desktop.browe() method to launch the default Web browser on the desktop of the operating system.
If you want to launch the default Web browser on the current desktop of the operating system, you can get the default Desktop object and call its browse() method. Here is a sample program:
/* DesktopWebBrowser.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.net.URI; public class DesktopWebBrowser { public static void main(String[] a) { try { URI uri = new URI("https://www.herongyang.com/Swing"); Desktop desktop = Desktop.getDesktop(); if (desktop != null) desktop.browse(uri); } catch (Exception e) { e.printStackTrace(); } } }
If you run this program, you will see the default Web browser is started with the given Website displayed. On my Windows 10 computer, I am getting the Edge browser launched.
On my macOS computer, I am getting the Safari browser 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