【问题标题】:dynamically adding hyperlinks to rtf file in JEditorPane在 JEditorPane 中动态添加超链接到 rtf 文件
【发布时间】:2015-08-10 17:20:17
【问题描述】:

我目前正在构建一个用 Java 编写的用于多窗口媒体注释工具的文字处理器。电影专业的学生可以撰写论文并将其嵌入多媒体剪辑的链接。

我希望用户能够突出显示 rtf 文档中的文本并创建指向项目中媒体文件的链接。单击该链接时,将在其相关窗口中显示媒体。

我想知道是否可以在 Java 的 rtf 文档中动态创建超链接?例如,在 Word 中是可能的。

目前我正在使用带有高级 RTF 编辑器工具包 (http://java-sl.com/advanced_rtf_editor_kit.html) 的 JEditorPane。我正在努力寻找任何解决方案。

非常感谢任何帮助或指针。

谢谢

编辑:

  • 代码,添加了@ Eric 答案中的第 1 部分和第 3 部分

     `item3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            //use FX thread to open FileChooser
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("create link");
                    String startDirectory = System.getProperty("user.home") + File.separator + "Pictures";
                    fileChooser.setInitialDirectory(new File(startDirectory));
                    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JPEG files (*.jpg)", "*.jpg");
                    FileChooser.ExtensionFilter extFilter2 = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png"); 
                    FileChooser.ExtensionFilter extFilter3 = new FileChooser.ExtensionFilter("JPG files (*.jpeg)", "*.jpeg"); 
                    fileChooser.getExtensionFilters().addAll(extFilter,extFilter2, extFilter3);
    
                    File imageFile = fileChooser.showOpenDialog(stage);
                    if(imageFile != null){
                        Image image = ImageViewerController.getImage(); 
    
                        try {               
                            image = new Image(imageFile.toURI().toURL().toExternalForm().toString());
    
    
                        int start = textArea.getSelectionStart();
                        int end = textArea.getSelectionEnd();   
                        textArea.getDocument().remove(start, end);  
                        String newString = "{\field{\*\fldinst HYPERLINK 'http://www.google.com/'}{\fldrslt http://www.google.com}}";
                        textArea.getDocument().insertString(start, newString , null);
                            textArea.addHyperlinkListener(new HyperlinkListener() {
                                    @Override
                                    public void hyperlinkUpdate(HyperlinkEvent hle) {
                                        if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
                                            System.out.println(hle.getURL());
                                            Desktop desktop = Desktop.getDesktop();
                                            try { 
                                                desktop.browse(hle.getURL().toURI());
                                            } catch (Exception ex) {
                                                ex.printStackTrace();
                                            }
                                        }
                                    }
                                });     
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }           
                    }
                }
    
           });`
    

【问题讨论】:

    标签: java swing rtf jeditorpane


    【解决方案1】:

    我认为您的问题有多个方面:

    1.替换Document中的选定文本:

    获取所选范围:

    int start = editorpane.getSelectionStart();
    int end = editorpane.getSelectionEnd();
    

    将文本替换为:

    editorpane.getDocument().remove(start,end);
    editorpane.getDocument().insertString(start, newString, null);
    

    注意:如果需要,请将null 替换为实际的属性集。

    2. 创建一个 RTF 格式的超链接。我认为this post 应有尽有。

    3. 对超链接点击做出反应:As explained in the docs,您必须在编辑器窗格中添加HyperlinkListener 才能打开相应的媒体。然而,这个工作的一个条件是当点击超链接时编辑器工具包会生成HyperlinkEvents。 HTML 文档肯定是这种情况,但由于您使用的是第 3 方库,我无法确认它是否会以相同的方式工作...

    【讨论】:

    • 嗨@Eric 谢谢你的详细回答。非常感激。我已经设法让第 1 部分和第 3 部分工作。我需要使用文档来调用 '.getEditor().remove(start,end)' 和 '.insertString(start, newString, null)' 方法,因为 JEditorPane 没有它们。但是,我遇到了 2 的问题。我认为这与正确转义 RTF 代码有关,因此可以将其作为字符串读取。
    • 嗨@Eric 忘了补充:我试过转义反斜杠和引号,但我只是得到了整个文本:{\field{*\fldinst HYPERLINK "google.com"}{\fldrslt @ 987654324@}} 而不是超链接。你能举例说明如何在字符串中使用 rtf。再次感谢您的帮助
    • 抱歉getEditor(),这是一个错字!我的意思是getDocument(),你发现了。关于字符串的格式,您可以将代码添加到您的问题中吗?此外,最好确认您正在使用的库能够处理超链接。我没有在您的链接之后看到太多文档...也许如果您使用 jd-eclipse 或类似工具打开 jar...?
    • 已添加代码。忽略文件文件选择器部分。它用于设置超链接媒体的文件路径。目前,只是试图让它使用一个简单、可靠的 URL。另外,我目前使用的是 Oracle RTF 编辑器,而不是高级编辑器。我的错。提前致谢
    • 关于字符串格式,你一定要像\ -> \\ 这样转义反斜杠。此外,您可能应该一劳永逸地添加一个超链接侦听器,而不是每次创建链接时都添加一个新的。
    猜你喜欢
    • 2016-03-15
    • 2012-03-05
    • 1970-01-01
    • 2012-10-28
    • 2016-02-15
    • 2010-11-15
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    相关资源
    最近更新 更多