在JEditorPane中发生HyperlinkEvent时如何取消对MouseEvent的响应?

tripofdream 2003-11-19 10:49:30
......
JEditorPane jp = new JEditorPane();
jp.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
....
}
});
jp.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e) {
....
}
});
如何在点击JEditorPane中的超链接时不执行mouseClicked()中的动作?
...全文
86 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tripofdream 2003-11-20
  • 打赏
  • 举报
回复
我现在的方案是延时200毫秒,正常情况不会有问题,但肯定会有不可预知的意外情况.
bluesmile979 2003-11-20
  • 打赏
  • 举报
回复
我不太清楚有没有现成的函数可以用,如果担心不太安全的话。楼主看看下面这种处理是否可行?100毫秒左右的损失应该是可以被忽略的,这样可以保证你的HyperlinkEvent肯定会先上来。

jp.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
sleep(50);
if(flag){
....
}
}
});
jp.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e) {
flag = false;
....
sleep(100);
flag = true;
}
});
tripofdream 2003-11-20
  • 打赏
  • 举报
回复
to up:
In my test,HyperlinkEvent was generated 10~30 milliseconds earlier before MouseEvent.
tripofdream 2003-11-20
  • 打赏
  • 举报
回复
原来的方案是用button的,不过觉得button破坏了界面的和协性。用右键与用户习惯不符。

其实我只是想知道有没有直接的解决方案,看来是没有结果了,先结帐吧。
bluesmile979 2003-11-20
  • 打赏
  • 举报
回复
如果只是为了关闭窗口,添加个button或者一个处理关闭的链接不可以么?

可以区分左右键的


if(e.getButton()==e.BUTTON2){
右键
}
wobelisk 2003-11-20
  • 打赏
  • 举报
回复
sorry, cannot type chinese on my computer
wobelisk 2003-11-20
  • 打赏
  • 举报
回复

I read the source, HyperlinkEvent is generated by mouseClick(). That means your mouseClick() will happen before hyperlinkUpdate(). Because a Mouse Click event will be generated first, then JEditorPane try to check if a link is clicked. If a link is clicked, a HyperlinkEvent will be dispatched and hyperlinkUpdate() is called.

Two suggestion:
1. try to use another framework, avoid processing mouseClick and hyperlinkUpdate at the same time
2. If have to, under mouseClick() you must check the mouse position is within a link before doing other things.
There are two types of link, <a href> and <map>. Copy the source code from
HTMLEditoreKit.LinkController can solve <a href>. But you cannot solve <map>, unless you write your own codes. Because to determine if it's a <map> link, HTMLDocument and java.swing.text.html.Map class are used. You're unlucky, Map is package-access class. So methods of Map, such as Map.getArea(), cannot be accessed in your codes because I don't think you'll put your code under java.swing.text.html package.

If no <map>, I can give you a solution. If you want to handle <map>, try to write your own code.
tripofdream 2003-11-20
  • 打赏
  • 举报
回复
比如在机器非常繁忙的时候。
mouseClicked()并没有区分左右键;这是一个产品信息的about窗口,单击鼠标则关闭窗口。
bluesmile979 2003-11-20
  • 打赏
  • 举报
回复
比如呢?会有什么意外?

为什么一定要响应左键点击事件呢?用右键不行么?
tripofdream 2003-11-19
  • 打赏
  • 举报
回复
我目前使用的解决方法正是楼上的方法,只是不知道这种方法是否可靠,即是否会有意外发生,所以想寻求正规的解决方案。
bluesmile979 2003-11-19
  • 打赏
  • 举报
回复
这里所谓的同时应该还是有先后的吧,就好像鼠标拖曳之前肯定会发生鼠标点击事件。如果HyperlinkEvent先于MouseEvent发生,那么简单设定一个flag HY事件里面改变flag的值,在mouse里面加个对flag的判断应该就可以。

如果猜测错误,那再说吧,会比较麻烦
wobelisk 2003-11-19
  • 打赏
  • 举报
回复
too hard.
tripofdream 2003-11-19
  • 打赏
  • 举报
回复
d
tripofdream 2003-11-19
  • 打赏
  • 举报
回复
to 楼上:
不是不想执行mouseClicked()中的动作,而是当鼠标点击的是JEditorPane中的超链接时不执行mouseClicked()中的动作,当在JEditorPane的其它地方点击时是要执行mouseClicked()中的动作的.
jaddy 2003-11-19
  • 打赏
  • 举报
回复
如果不想执行mouseClicked()中的动作,那为什么要注册它呢。

62,610

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧