java - dynamically adding hyperlinks to rtf file in JEditorPane -
i building word processor use in multi-window media annotation tool, written in java. film students write essays , embed them links multimedia clips.
i want user able highlight text in rtf document , create link media file in project. when clicked link display media in associated window.
i know if possible dynamically create hyperlinks in rtf documents in java? possible in word, example.
at moment using jeditorpane advanced rtf editor kit (http://java-sl.com/advanced_rtf_editor_kit.html). struggling find sort of solution.
any or pointers appreciated.
thanks
edit:
code, parts 1 & 3 @ eric's answer added
`item3.addactionlistener(new actionlistener() { @override public void actionperformed(java.awt.event.actionevent e) { //use fx thread 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(); } } } });`
i think there various parts in question:
1. replace selected text in document
:
get selected range with:
int start = editorpane.getselectionstart(); int end = editorpane.getselectionend();
replace text with:
editorpane.getdocument().remove(start,end); editorpane.getdocument().insertstring(start, newstring, null);
note: replace null
actual attribute set if needed.
2. create rtf-formatted hyperlink. think this post has everything.
3. react hyperlink clicks: as explained in docs, must add hyperlinklistener
editor pane open corresponding media. condition work editor kit generates hyperlinkevent
s when hyperlinks clicked. case html documents, since using 3rd party library, cannot confirm work same way...
Comments
Post a Comment