Ani*_*rma 1 java desktop-application javafx javafx-2
我正在开发一个应用程序,其中我有一些链接添加到Listview,这些链接将继续在运行时添加某些条件.所以我找不到的是如何在单击特定链接时打开URL的方法.
这是将链接添加到列表视图的代码
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);
}
Run Code Online (Sandbox Code Playgroud)
我知道我需要使用这样的东西在浏览器中点击链接时打开一个网址
getHostServices().showDocument(link.getText());
Run Code Online (Sandbox Code Playgroud)
但我不知道如何监听/跟踪不同链接的点击事件
我为你做了一个很小的例子申请,
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果在TextField中输入新URL并单击Button,则新链接将添加到LinkList中,并将显示在ListView上.每次添加新链接时,.setOnAction()都会使用正确的URL设置方法.
也许您可以将此作为进一步开发应用程序的起点.
帕特里克
我在超链接上放了一个工具提示,并从那里读取了URL,似乎可以正常工作。即:
Hyperlink hl = new Hyperlink(sometext);
hl.setTooltip(new Tooltip(theurlhere);
hl.setOnAction((ActionEvent event) -> {
Hyperlink h = (Hyperlink) event.getTarget();
String s = h.getTooltip().getText();
getHostServices.showDocument(s);
event.consume();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13021 次 |
| 最近记录: |