JEdi​​torPane 中使用 replaceSelection 方法的可点击 HTML 链接 [英] Clickable HTML link in JEditorPane using replaceSelection method

查看:16
本文介绍了JEdi​​torPane 中使用 replaceSelection 方法的可点击 HTML 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JEditorPane 中搜索了如何制作可点击的链接,我发现了这个问题:

<块引用>

.
");返回 sb.toString();}私有静态无效显示()抛出 HeadlessException {JEdi​​torPane jep = new JEditorPane();jep.setContentType("text/html");StringBuilder sb = new StringBuilder();sb.append("<b>欢迎</b>:<br><hr>");for (int i = 1; i <= 3; i++) {sb.append(create(i));}sb.append("


");jep.setText(sb.toString());jep.setEditable(false);jep.addHyperlinkListener(new HyperlinkListener() {@覆盖public void hyperlinkUpdate(HyperlinkEvent e) {if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {System.out.println(e.getURL());桌面桌面 = Desktop.getDesktop();尝试 {desktop.browse(e.getURL().toURI());} 捕捉(异常前){ex.printStackTrace();}}}});JFrame f = new JFrame("HyperlinkListener");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.add(jep);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}}

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);
    }
}

这篇关于JEdi​​torPane 中使用 replaceSelection 方法的可点击 HTML 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆