Open a Link in Browser with Java Button

Open a link in browser with java button?

Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.

public static boolean openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}

public static boolean openWebpage(URL url) {
try {
return openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return false;
}

open a link in different browser java swing

Generally speaking, to open a link in the user's default browser you should really be using the more modern approach:

String url = "www.stackoverflow.com";
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.browse(url);

No need to mess about with working out what OS you are running on (as your linked example attempts to do). Far better to let java.awt.Desktop take care of finding an appropriate browser or application to open the URL (see the documentation for more details).

There's also a part of the API that gracefully handles permissions and 'unusual' OS setups. If there is the possibility that your code will run under a restricted security policy or on a platform that may not have a browser, then you can check up front rather than wait for the exception from the call to browse.

// check if java.awt.Desktop is available on the current platform
java.awt.Desktop.isDesktopSupported();

// check the current platform and security policy will let you browse to a url
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.isSupported(Desktop.Action.BROWSE);

Easiest way to have button open browser to specific URL

This line should open your built-in browser, with the specified url:

    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));

Your Activity should have parts like this:

//define class variables here
Button btn;

protected void onCreate(Bundle savedInstanceState)
{
//some code of yours
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
//more code of yours
}

//whatever else you have in your source code

public void onClick(View v)
{
//handle the click events here, in this case open www.google.com with the default browser
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}

It might not be 100% accurate syntax, since I did just write this on my own, but you get the idea.

Java SE: Open Web Page and Click a Button

You may be looking for HtmlUnit -- a "GUI-Less browser for Java programs".

Here's a sample code that opens google.com, searches for "htmlunit" using the form and prints the number of results.

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

public class HtmlUnitFormExample {
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://www.google.com");

HtmlInput searchBox = page.getElementByName("q");
searchBox.setValueAttribute("htmlunit");

HtmlSubmitInput googleSearchSubmitButton =
page.getElementByName("btnG"); // sometimes it's "btnK"
page=googleSearchSubmitButton.click();

HtmlDivision resultStatsDiv =
page.getFirstByXPath("//div[@id='resultStats']");

System.out.println(resultStatsDiv.asText()); // About 309,000 results
webClient.closeAllWindows();
}
}

Other options are:

  • Selenium: Will open a browser like Firefox and operate it.
  • Watij: Also will open a browser, but in its own window.
  • Jsoup: Good parser. No JavaScript, though.

Java GUI - Web Browser and Opening Link

To open a url in the system's web browser you can use java.awt.Desktop.browse(URI). This allows you to keep your Java code platform independent, and even allows you to check to see if an operation is supported before trying to use it.

To load a web page within Java, I've had some success using the JavaFX WebView.



Related Topics



Leave a reply



Submit