单击JavaFX中的超链接时,应在浏览器中打开相关URL [英] When Click on Hyperlinks in JavaFX ,a relevant URL should open in browser

查看:113
本文介绍了单击JavaFX中的超链接时,应在浏览器中打开相关URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中我在Listview中添加了一些链接,这些链接将在运行时继续在某些条件下添加。所以我找不到的是一种在点击时如何打开URL的方法一个特定的链接。

I am developing an application in which I have some links added to the Listview and these links will keep on adding at runtime on some condition.So what I can't find is a way to how to open a url when clicking on a particular link.

这是将链接添加到列表视图的代码

This is the code for adding links into list view

    if(counter==1)
                {
                    Task task2 = new Task<Void>() {
                          @Override
                          public Void call() throws Exception {

                              Platform.runLater(new Runnable() {
                                public void run() {
                                     link=new Hyperlink(val);
                                    link.setStyle("-fx-border-style: none;");
                                    items.add(link);
                                    listview.setItems(items);



                                }
                              });


                            return null;

                          }
                        };
                        Thread th = new Thread(task2);
                        th.setDaemon(true);
                        th.start(); 
                        Thread.sleep(1000);


                }

我知道我需要使用像这是在点击链接时在浏览器中打开网址

I know I need to use something like this to open a url in browser when click on link

 getHostServices().showDocument(link.getText()); 

但我不知道如何收听/跟踪不同链接的点击事件

but I don't know how to listen/track the click event for different links

推荐答案

我为您做了一个非常小的示例应用程序,

I made a very small example Application for you,

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListList extends Application {

    final ListView listView = new ListView();
    @Override
    public void start(Stage primaryStage) {

        List<Hyperlink> links = new ArrayList<>();

        AnchorPane pane = new AnchorPane();
        VBox vBox = new VBox();
        final Hyperlink link = new Hyperlink("http://blog.professional-webworkx.de");
        Hyperlink link2= new Hyperlink("http://www.stackoverflow.com");

        links.add(link);
        links.add(link2);

        for(final Hyperlink hyperlink : links) {
            hyperlink.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent t) {
                    getHostServices().showDocument(hyperlink.getText());
                }
            });
        }

        listView.getItems().addAll(links);
        HBox hBox = new HBox();
        final TextField urlField = new TextField();
        Button b = new Button("Add Links");
        hBox.getChildren().addAll(b, urlField);

        b.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                addLink(urlField.getText().trim());
                urlField.clear();
            }
        });
        vBox.getChildren().add(hBox);
        vBox.getChildren().add(listView);
        pane.getChildren().add(vBox);
        Scene scene = new Scene(pane, 800, 600);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private void addLink(final String url) {
        final Hyperlink link = new Hyperlink(url);
        link.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                getHostServices().showDocument(link.getText());
                //openBrowser(link.getText());
            }

        });
        listView.getItems().add(link);
    }

    private void openBrowser(final String url) {
        getHostServices().showDocument(url);
    }
}

如果您在TextField中输入新网址并点击在Button上,新链接将添加到您的LinkList,并将显示在ListView上。每次添加新链接时,都会设置 .setOnAction()方法,并打开正确的URL。

If you enter a new URL in the TextField and click on the Button, the new Link will be added to your LinkList and will be displayed on the ListView. Everytime you add a new Link, the .setOnAction() Method is set with the right URL to open.

也许您可以将此作为进一步开发应用程序的起点。

Maybe you can use this as starting point for further developing your app.

Patrick

这篇关于单击JavaFX中的超链接时,应在浏览器中打开相关URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆