У меня есть JTextPane, содержащий гиперссылки. Когда они доступны для редактирования, гиперссылки не активируются, и курсор не меняет состояние при наведении на них курсора (например, на руку). Когда он не редактируемый, гиперссылки активируются, а курсор меняет состояние при наведении на них. Идеально подходит.
Вот проблема, если курсор уже находится над JTextPane при изменении его редактируемости, курсор не обновляется. Единственный способ (который я знаю) для обновления курсора - это переместить его.
Даже если курсор не может изменить состояние, HyperlinkListeners увидит событие ACTIVATED, когда нажата мышь, а JTextPane недоступна для редактирования. Есть ли способ заставить курсор переоценить, в каком состоянии он должен быть, и обновить себя, когда я изменю состояние панели под ним?
По сути, я хочу, чтобы значок курсора всегда соответствовал тому, что будет происходить при нажатии мыши. Да, это граничный случай, но он все еще довольно раздражающий.
Вот пример кода для игры.
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();
}
});
}
}