简体   繁体   English

单击超链接时打开文件-Java FX

[英]Opening file when hyperlink is clicked - Java FX

I have hyperlink created in a textflow container in Java FX. 我在Java FX的文本流容器中创建了超链接。 The code provided below only opens the last hyperlinked file even when the preceding links are clicked. 即使单击了前面的链接,下面提供的代码也只会打开最后一个超链接的文件。 I think the problem is in the iteration. 我认为问题出在迭代中。 Kindly bear with me as I am still fresh on Java. 敬请谅解,因为我仍然对Java感到陌生。 ` `

String[] splits = lessonResources.split("\\s+");

       for(String s: splits){
             link = new Hyperlink(s);
             lessonResourcesTextFlow.getChildren().add(link);
             linked = new File(s);   
             link.setOnAction((ActionEvent e) -> {   
                  try {
                         if(Desktop.isDesktopSupported()){
                               try {
                                    Desktop.getDesktop().open(linked);
                                } catch (IOException ex) {
                                       Logger.getLogger(LessonPlanController.class.getName   ()).log(Level.SEVERE, null, ex);
                                }
                            }
                     } catch (Exception e) {
                      System.out.println(e);
                   }
               }); 
         }

Including the file constructor within the link event handler solved my problem. 将文件构造函数包含在链接事件处理程序中解决了我的问题。

String[] splits = lessonResources.split("\\s+");
    for(String s: splits){
            link = new Hyperlink(s);
            lessonResourcesTextFlow.getChildren().add(link);


            link.setOnAction((ActionEvent e) -> {  
                    linked = new File(s); 
                            try {
                                if(Desktop.isDesktopSupported()){
                                    try {
                                        Desktop.getDesktop().open(linked);
                                    } catch (IOException ex) {
                                        Logger.getLogger(LessonPlanController.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                }

                            } catch (Exception e) {
                                System.out.println(e);
                            }
            }); 

    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM