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:
- Familiar with Java swing development, go oracle Java Swing to get the tutorial reference.
- Read the previous three tutorials on JSwing.
- Familiar with the operation of Apache NetBeans.
- Familiar with the timing and object of caretUpdate be called.
Create a JSwing Form
- 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.
- Change the properties of the component as shown in the table below.
Components text actionCommand name jLbael1 URL url jLbael2 Before before jLbael3 After after jTextField1 urlAddress jTextField2 oldValue jTextField3 newValue jButton1 Go Go Go - After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.
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
- 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.
Exercises
- 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.