java - Clickable HTML link in JEditorPane But using replaceSelcetion methode -


i searched of how make clickable links in jeditorpane , found question

is possible create programs in java create text link in chrome?

it useful code use repetition statement

jeditorpane jep = new jeditorpane(); jep.setcontenttype("text/html"); jep.seteditable(true);// because .replaceselection can't work disabled edit ( int = 1; <= 3; i++ ){     jep.replaceselection(         "welcome <a href='https://stackoverflow.com/'>stackoverflow </a>."); } jep.seteditable(false); 

and show me text without clickable links how i'm going make right , need replaceselection method

using replaceselection() on htmldocument inserts raw string; want insert html anchor tag. can,

  • manage raw html text yourself, shown below, , let settext() handle parsing.

  • use 1 of existing htmleditorkit nested actions.

  • use 1 of custom approaches seen here.

image

import java.awt.desktop; import java.awt.headlessexception; import javax.swing.jeditorpane; import javax.swing.jframe; import javax.swing.swingutilities; import javax.swing.event.hyperlinkevent; import javax.swing.event.hyperlinklistener;  /**  * @see https://stackoverflow.com/a/16447176/230513  * @see https://stackoverflow.com/a/14170141/230513  */ public class test {      public static void main(string[] argv) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 display();             }         });     }      private static string create(int i) {         stringbuilder sb = new stringbuilder();         sb.append("welcome <a href=");         sb.append("'http://www.example.com'>example ");         sb.append(i);         sb.append("</a>.<br>");         return sb.tostring();     }      private static void display() throws headlessexception {         jeditorpane jep = new jeditorpane();         jep.setcontenttype("text/html");         stringbuilder sb = new stringbuilder();         sb.append("<b>welcome</b>:<br><hr>");         (int = 1; <= 3; i++) {             sb.append(create(i));         }         sb.append("<hr>");         jep.settext(sb.tostring());         jep.seteditable(false);         jep.addhyperlinklistener(new hyperlinklistener() {             @override             public void hyperlinkupdate(hyperlinkevent e) {                 if (hyperlinkevent.eventtype.activated.equals(e.geteventtype())) {                     system.out.println(e.geturl());                     desktop desktop = desktop.getdesktop();                     try {                         desktop.browse(e.geturl().touri());                     } catch (exception ex) {                         ex.printstacktrace();                     }                 }             }         });          jframe f = new jframe("hyperlinklistener");         f.setdefaultcloseoperation(jframe.exit_on_close);         f.add(jep);         f.pack();         f.setlocationrelativeto(null);         f.setvisible(true);     } } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -