1

I am currently building a word processor for use in a multi-window media annotation tool, written in Java. It is for film students to write essays and embed them with links to multimedia clips.

I want the user to be able to highlight text in an rtf document and create a link to a media file in the project. When clicked the link will display the media in its associated window.

I would like to know if it is possible to dynamically create hyperlinks in rtf documents in Java? As is possible in Word, for example.

At the moment I am using a JEditorPane with the Advanced RTF Editor Kit (http://java-sl.com/advanced_rtf_editor_kit.html). I am struggling to find any sort of a solution.

Any help or pointers greatly appreciated.

Thanks

Edit:

  • code, with parts 1 & 3 from @ Eric's answer added

     `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();
                        }           
                    }
                }
    
           });`
    
Claus Harbach
  • 31
  • 1
  • 5

1 Answers1

2

I think there are various parts in your question:

1. Replace selected text in a Document:

Get the selected range with:

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

Replace the text with:

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

Note: replace null with actual attribute set if needed.

2. Create a RTF-formatted hyperlink. I think this post has everything.

3. React to hyperlink clicks: As explained in the docs, you must add a HyperlinkListener to the editor pane to open the corresponding media. However a condition for this to work is that the editor kit generates HyperlinkEvents when hyperlinks are clicked. This is definitely the case for HTML documents, but since you are using a 3rd party library, I cannot confirm it will work the same way...

Community
  • 1
  • 1
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
  • Hi @Eric Thanks for you detailed answer. Much appreciated. I have managed to get parts 1 and 3 working. I needed to use the document to call the '.getEditor().remove(start,end)' and '.insertString(start, newString, null)' methods as the JEditorPane does not have them. However, I am having trouble with 2. I think it has something to do with correctly escaping the RTF code so it can be read as a string. – Claus Harbach Aug 09 '15 at 21:12
  • Hi @Eric Forgot to add: I have tried escaping backslashes and quotes but am just getting the entire text of this: {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}} rather then a hyperlink. Can you give an example of how to use rtf in a string. Thanks again for your help – Claus Harbach Aug 09 '15 at 21:19
  • Sorry about the `getEditor()`, it was a typo! I meant `getDocument()`, as you found out. Regarding the formatting of the string, can you add your code to your question? Also, it would be good to confirm that the library you are using is able to process hyperlinks. I didn't see much documentation following your link... Maybe if you opened the jar with jd-eclipse or similar tool...? – Eric Leibenguth Aug 09 '15 at 22:20
  • Have added the code. Ignore the file filechooser section. It's for setting a filepath to the hyperlinked media. At the moment, just trying to get it working with a simple, reliable URL. Also, I am using the Oracle RTF Editor at the moment, not the Advanced Editor. My mistake. Thanks in advance – Claus Harbach Aug 10 '15 at 09:23
  • Regarding the string formatting, you definitely have to escape the backslashes like this `\ -> \\ `. Also you should probably add a single hyperlink listener once and for all, not add a new one every time you create a link. – Eric Leibenguth Aug 10 '15 at 09:47
  • Looking at the simple RTFEditorKit, it doesn't look like it supports the `HYPERLINK` keyword... http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/rtf/RTFParser.java – Eric Leibenguth Aug 10 '15 at 09:59
  • Ah, then it's not possible without some serious tweaks. Thanks for your help. – Claus Harbach Aug 10 '15 at 10:34