R
R
Rishat Sultanov2016-12-15 06:33:14
Java
Rishat Sultanov, 2016-12-15 06:33:14

How to open a link in a browser on a button click?

Good afternoon.
How to implement opening a link in the browser by clicking on the button.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-12-15
@rishatss

import java.awt.Desktop;
import java.net.URI;

public class Main {
    public static void main(final String args[]) {
        URI uri = new URI("http://toster.ru");
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question