Open a url in the default browser in JSwing

In many desktop applications, getting the last version of the system by visiting the website is very common. In this tutorial, We will teach you how to open the specified URL in the default browser in JSwing applications. To record changes to the input URL, we use the event handler of caretUpdate to display the old and new values.

You will learn how to:

  • Open a web page by a Java method - browse.
  • Create an event handler of JTextField - caretUpdate.

 

Prerequisites

Before you begin to read this tutorial, you must:

  1. Familiar with Java swing development, go oracle Java Swing to get the tutorial reference.
  2. Read the previous three tutorials on JSwing.
  3. Familiar with the operation of Apache NetBeans.
  4. Familiar with the timing and object of caretUpdate be called.

 

Create a JSwing Form

  1. Create a JFrame class named OpenWebPage on the form of the Source Packages, and then add components to the JFrame. The tree diagram is shown in the following figure.

    OpenWebpage tree view

  2. Change the properties of the component as shown in the table below.
    ComponentstextactionCommandname
    jLbael1URL url
    jLbael2Before before
    jLbael3After after
    jTextField1  urlAddress
    jTextField2  oldValue
    jTextField3  newValue
    jButton1GoGoGo
  3. After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.

    OpenWebpage Form

Create a JSwing App

Create a Java class named OpenWebPage on the app of the Source Packages, and then add code to the Class.

package app;

import jswing.application.*;

public final class OpenWebPage extends App {

    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Form.open("form.OpenWebPage","OpenWebPage");
                UserInterface.initialize("app.OpenWebPage",Form.getCurrentForm());
            }
        });
    }
    
    @Override
    public void caretUpdate(java.awt.Component component) {
        if (component.equals(Form.getCurrentForm().getComponentByName("urlAddress"))) {
            String newValue = ((javax.swing.JTextField)Form.getCurrentForm().getComponentByName("urlAddress")).getText();
            javax.swing.JTextField oldTextField = ((javax.swing.JTextField)Form.getCurrentForm().getComponentByName("oldValue"));
            javax.swing.JTextField newTextField = ((javax.swing.JTextField)Form.getCurrentForm().getComponentByName("newValue"));
            oldTextField.setText(newTextField.getText());
            newTextField.setText(newValue);
        }            
    }
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e){
        String action = e.getActionCommand();
        switch(action){
        case "Go":
            try {
                java.net.URI uri = new java.net.URI(((javax.swing.JTextField)Form.getCurrentForm().getComponentByName("urlAddress")).getText());
                java.awt.Desktop.getDesktop().browse(uri);
                System.out.println("Web page opened in browser:" + uri);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
            break;
        }
    }
}

 

Code Explained

  1. caretUpdate.

    caretUpdate is called when the caret position is updated. In this tutorial, we detect changes in text on jTextField1 to catch old and new values by the event handler of caretUpdate.

 

Run application

After specifying the URL, when you click Go, the browser will get opened with the specified URL.

OpenWebpage

 

Exercises

  1. Recoding the application "Make an about page on the desktop application" that makes it can open Hyperlinks.

Next steps

Congratulations! Once you've completed this tutorial, you can easily migrate to your applications. Click download source code or next for more tutorials.

Share this page

Let more people know about this site, please share