How to open a web link on a java button?

Asked

Viewed 1,635 times

1

Well, I created a class :


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


public class AbrirLink {
public static void main(String[] args){

    try{
        URI link = new URI("www.google.com");
        Desktop.getDesktop().browse(link);
    }catch(Exception erro){
            System.out.println(erro);
        }
    }

}

I wanted to know how to call her inside a button, so when I click the button I opened the web page.

I created an Actionperformed on the Button and put the following code:

AbrirLink ab = new AbrirLink();

But I think there’s something else I need to do?

2 answers

1


This way it worked: If you want to open a link by clicking on an Jmenubar just create an event of Mouseclicked in that item and put that Code there:

String[] args; try{ URI link = new URI("www.google.com"); Desktop.getDesktop().browse(link); }catch(Exception erro){ System.out.println(erro); }

Should you want a Jbuttom open a link by clicking on it, create an Actionperformed on the Button and put the code above.

  • That’s what I told you to do... URI link = new URI("www.google.com"); Desktop.getDesktop().browse(link);

0

Use the method browse(URI uri) of class Desktop!

This method starts the default browser to display a URI (SIMILAR TO A URL).

Example:

String valor = tableLinks.getModel().getValueAt(row, 0).toString();
java.awt.Desktop.getDesktop().browse( new java.net.URI( "http://www." + valor  ));

NOTE

For more information visit official documentation of oracle.

  • I didn’t understand it very well, like I used this Rowse(URI) and this get Desktop right there in the class I did, until if I run only this class it opens the browser normally, I wanted to know if type has how to call this class on my button to click on it open the web link, if yes, what command should I do in the action of my button to be able to execute this class by clicking the ? Thanks in advance

  • I actually did some tests here and I got it, it wasn’t working because I was putting in a "Jmenubar button" and not a normal button, vlw

  • @Vinicius alright, put the code there for us in your answer.

  • Blza, I put it there

Browser other questions tagged

You are not signed in. Login or sign up in order to post.