Abrir link no navegador(RESOLVIDO)

2 respostas
java
rodrigosnantes

senhores como posso abrir um link no navegador atravez de um link de um jtable.

ex. tenho um jtable que armazena links, em texto de internet.

Quero conseguir abrir o link que esta no jtable, como posso fazer?

2 Respostas

TerraSkilll

Use o método Desktop.browse(). Ele abre uma URI (similar à uma URL) no navegador padrão do sistema.

Exemplo:

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

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

Referência: http://stackoverflow.com/questions/10967451/open-a-link-in-browser-with-java-button

Abraço.

rodrigosnantes

consegui fazer aqui, de uma forma um pouco diferente

public void abrirLink() throws IOException {

int row = tableLinks.getSelectedRow();
    tableLinks.getValueAt(row, 0).toString();
    String valor = tableLinks.getModel().getValueAt(row, 0).toString();
    System.out.println(valor);

    try {
        java.awt.Desktop.getDesktop().browse( new java.net.URI( "http://www." + valor  ) );
    } catch (URISyntaxException ex) {
        Logger.getLogger(CadastroLink.class.getName()).log(Level.SEVERE, null, ex);
    }
}

mais vlw amigo!

abrass

Criado 23 de setembro de 2016
Ultima resposta 25 de set. de 2016
Respostas 2
Participantes 2