调用JEditor.setEditable()时,更改光标以悬停在超链接上

时间:2011-07-23 22:27:54

标签: java swing hyperlink cursor mouse

我有一个包含超链接的JTextPane。当它是可编辑的时,超链接不可点击,并且当鼠标悬停在它们上时光标不会改变状态(例如,到手)。如果可编辑,超链接 可点击,光标 在将鼠标悬停在其上时会改变状态。完美。

问题是,如果光标在其可编辑性更改时已经悬停在JTextPane上,则光标不会更新。光标更新的唯一方法(我知道)是移动它。

即使光标可能没有改变状态,当按下鼠标并且JTextPane不可编辑时,HyperlinkListener将看到一个ACTIVATED事件。有没有办法强制光标重新评估它应该处于什么状态,并在我更改其下面板的状态时自行更新?

基本上,我希望光标图标始终对应于按下鼠标时会发生的情况。是的,这是一个边界情况,但它仍然很烦人。

以下是一些示例代码。

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Every five seconds the editability of the JTextPane is changed. If the
 * editability changes to false when the mouse is hovering over the hyperlink,
 * the cursor will not change to a hand until it is moved off and then back
 * onto the hyperlink. If the editability changes to true when the mouse is
 * hovering over the hyperlink, the cursor will not change back to normal
 * until it is moved slightly.
 * 
 * I want the cursor to update without having to move the mouse when it's
 * already hovering over a hyperlink.
 */
@SuppressWarnings("serial")
public class HyperlinkCursorProblem extends JFrame {

  public HyperlinkCursorProblem(String title) {
    super(title);

    Container pane = getContentPane();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

    final JLabel editabilityLabel = new JLabel("Editable");
    pane.add(editabilityLabel);

    final JTextPane textPane = new JTextPane();
    textPane.setContentType("text/html");

    StringBuilder text = new StringBuilder();
    text.append("<html><body>");
    text.append("<a href=\"http://www.example.com/\">");
    text.append("Click here for fun examples.");
    text.append("</a>");
    text.append("</body></html>");
    textPane.setText(text.toString());
    textPane.addHyperlinkListener(new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          System.out.println("Going to " + e.getURL());
        }
      }
    });
    textPane.setPreferredSize(new Dimension(500, 250));
    pane.add(textPane);

    new Thread(new Runnable() {
      public void run() {
        while (true) {
          try {
            Thread.sleep(5000);

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    boolean editable = !textPane.isEditable();
                    textPane.setEditable(editable);
                    editabilityLabel.setText(editable ? "Editable" : "Not Editable");
                }
            });
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }).start();
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new HyperlinkCursorProblem("Large File Mover");

        // Display the window.
        frame.setVisible(true);
        frame.pack();
      }
    });
  }
}

2 个答案:

答案 0 :(得分:4)

阅读Processing hyperlinks in editable JEditorPane with HTMLEditorKit


其中的代码将生成一个可编辑的JTextPane,当鼠标悬停在超链接上时光标会发生变化。

enter image description here

答案 1 :(得分:1)

编辑:

@masson你是说Cursor MouseCursor来自Native OS,然后没有什么复杂只是跟随this线程