简体   繁体   中英

Clickable HTML link in JEditorPane using replaceSelection method

I searched how to make clickable links in JEditorPane , and I found this question:

Is it possible to create programs in Java that create text to link in Chrome?

It was very useful, but my code uses a repetition statement to add links in a loop:

JEditorPane jep = new JEditorPane();
jep.setContentType("text/html");
jep.setEditable(true);// Because replaceSelection can't work with disabled edit
for ( int i = 1; i <= 3; i++ ){
    jep.replaceSelection(
        "Welcome to <a href='https://stackoverflow.com/'>StackOverflow i </a>.");
}
jep.setEditable(false);

Now it shows me just text without clickable links. How am I going to make it right? I really need replaceSelection method.

Using replaceSelection() on an HTMLDocument inserts the raw string; you want to insert an HTML anchor tag. You can,

  • Manage the raw HTML text yourself, a shown below, and let setText() handle the parsing.

  • Use one of the existing HTMLEditorKit nested actions.

  • Use one of the custom approaches seen here .

图片

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 to <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>");
        for (int i = 1; i <= 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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