gpt4 book ai didi

java - 在可编辑的 EditorPane 上添加 HyperlinkListener

转载 作者:行者123 更新时间:2023-11-30 10:59:26 25 4
gpt4 key购买 nike

JEditorPane editorPane;
editorPane.addHyperlinkListener(...)
如果 editorPane 设置为 setEditable(true)

不会被触发。

因此,我尝试使用 FocusListener 动态修改 editable 属性:

public void createGui() {
SwingUtilities.invokeLater(() -> {
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<a href='http://google.com'>click me!</a>");

editorPane.setEditable(false);

editorPane.addFocusListener(new FocusListener() {
@Override public void focusGained(FocusEvent e) {
editorPane.setEditable(true); // <-- EDITABLE WHILE FOCUSSED
}
@Override public void focusLost(FocusEvent e) {
editorPane.setEditable(false); // <-- NON-EDITABLE WHILE NON-FOCUSSED
}
});

editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override public void hyperlinkUpdate(HyperlinkEvent e) {
System.out.println(e.getEventType().toString());
}
});

JButton someOtherSwingElement = new JButton("click me to remove focus from editorPane");
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new GridLayout());
jp.add(editorPane);
jp.add(someOtherSwingElement);
jf.add(jp);
jf.pack();
jf.setVisible(true);
}
}

但似乎focusGained()的内容会总是hyperlinkUpdate之前执行,无论前者需要多长时间。为什么?

有什么方法可以解决这个问题?

最佳答案

来自editable JEditorPane hypelinks

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;

public class URLEditorPane {

public static String HTML="<html>\n" +
"<body>\n" +
"Click on the link in the editale JEditorPane <br>\n" +
// "<a href=\"http://java.sun.com\">\nlink</a>" +
"<a href=\"file:///c:/temp/test.html\">\nlink</a>" +
"</body>\n" +
"</html>";

boolean isNeedCursorChange=true;
JTextPane edit=new JTextPane() {
public void setCursor(Cursor cursor) {
if (isNeedCursorChange) {
super.setCursor(cursor);
}
}
};

public URLEditorPane() {
JFrame frame=new JFrame("Click on Links in editable JEditorPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(edit);
MyHTMLEditorKit kit=new MyHTMLEditorKit();
// HTMLEditorKit kit=new HTMLEditorKit();

edit.setEditorKit(kit);
// edit.setEditable(false);

edit.setText(HTML);
edit.addHyperlinkListener(new HTMLListener());
frame.setSize(500,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) throws Exception {
new URLEditorPane();
}

private class HTMLListener implements HyperlinkListener {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
edit.setPage(e.getURL());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

public class MyHTMLEditorKit extends HTMLEditorKit {

MyLinkController handler=new MyLinkController();
public void install(JEditorPane c) {
MouseListener[] oldMouseListeners=c.getMouseListeners();
MouseMotionListener[] oldMouseMotionListeners=c.getMouseMotionListeners();
super.install(c);
//the following code removes link handler added by original
//HTMLEditorKit

for (MouseListener l: c.getMouseListeners()) {
c.removeMouseListener(l);
}
for (MouseListener l: oldMouseListeners) {
c.addMouseListener(l);
}

for (MouseMotionListener l: c.getMouseMotionListeners()) {
c.removeMouseMotionListener(l);
}
for (MouseMotionListener l: oldMouseMotionListeners) {
c.addMouseMotionListener(l);
}

//add out link handler instead of removed one
c.addMouseListener(handler);
c.addMouseMotionListener(handler);
}

public class MyLinkController extends LinkController {

public void mouseClicked(MouseEvent e) {
JEditorPane editor = (JEditorPane) e.getSource();

if (editor.isEditable() && SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount()==2) {
editor.setEditable(false);
super.mouseClicked(e);
editor.setEditable(true);
}
}

}
public void mouseMoved(MouseEvent e) {
JEditorPane editor = (JEditorPane) e.getSource();

if (editor.isEditable()) {
isNeedCursorChange=false;
editor.setEditable(false);
isNeedCursorChange=true;
super.mouseMoved(e);
isNeedCursorChange=false;
editor.setEditable(true);
isNeedCursorChange=true;
}
}

}
}
}

关于java - 在可编辑的 EditorPane 上添加 HyperlinkListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com