skyyoung:刚才个问题还没解决(requestFocus)

gdsean 2001-10-31 11:20:18
jTextUrl1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){}
public void focusLost(FocusEvent e){
if(jTextUrl1.getText().trim().compareTo("")==0){
JOptionPane.showMessageDialog(FrameSiteInfo.this,"不能为空");
jTextUrl1.requestFocus();
return;
}
为什么showMessageDialog总是出现?原先连续出现两次,现在(没修改什么)居然重复触发focusLost了,怎么解决?
...全文
253 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdsean 2001-11-01
  • 打赏
  • 举报
回复
好家伙,原来你也在这里泡!我好像没怎么见过你在。。。
香港好啊,唉海峰又去了南海,我好像越来越孤独咯,有没有
时间回来啊,像以前一样回学校去
fenghx 2001-11-01
  • 打赏
  • 举报
回复
快了,可能两个星期后就要走了。
gdsean 2001-11-01
  • 打赏
  • 举报
回复
ps:翔哥现在去了香港工作了吗?
gdsean 2001-11-01
  • 打赏
  • 举报
回复
谢了给分!
skyyoung 2001-11-01
  • 打赏
  • 举报
回复
是瓜!
gdsean 2001-11-01
  • 打赏
  • 举报
回复
唉抄得好,可惜把我的愿望给断了,一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?一定要setText改变textfield的值吗?
skyyoung 2001-11-01
  • 打赏
  • 举报
回复
当然不是我写的啦。
但要学会抄!哈哈。
gdsean 2001-11-01
  • 打赏
  • 举报
回复
我只看最后一段,发现一定要setText改变textfield的值,真失败!
真的必须这样做吗?这东西是你写的吗?原来你的e文那么厉害!!
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
我没有写代码,能发给我吗。shmilu@sina.com
gdsean 2001-10-31
  • 打赏
  • 举报
回复
也不行,有时候是一定要打开的,你试过没有?也许是bug
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
设置一个布尔值吧,如果打开了一个窗口,就设为TRUE,下次就不用在打开。
gdsean 2001-10-31
  • 打赏
  • 举报
回复
因为getText()返回的是""字符串,然后我把焦点再设置到jTextUrl1让重新输入,
现在程序结果和我目的不同了,没来得及重新输入又回到focusLost方法里面了,
当然jTextUrl1的内容依然是""
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
也既是话,每触发focusLost,就会通过if(jTextUrl1.getText().trim().compareTo("")==0),为什么这个条件为真!?
gdsean 2001-10-31
  • 打赏
  • 举报
回复
不行,现在问题是:重复触发focusLost
我按完对话框的ok,马上再弹出来一个
gdsean 2001-10-31
  • 打赏
  • 举报
回复
不行,现在问题是:重复触发focusLost
我按完对话框的ok,马上再弹出来一个
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
getText
public String getText()
Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException.
Returns:
the text
See Also:
setText(java.lang.String)

应该为
String str = jTextUrl1.getText();
if(str == null && "".equals(str.trim()))
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
if(jTextUrl1.getText().trim().compareTo("")==0)
改为
if("".equals(jTextUrl1.getText().trim()))看看
skyyoung 2001-10-31
  • 打赏
  • 举报
回复
note:

Be very careful on this type of behavior...

First, listeners are intended to be completely independent of each other. There are two main reasons for this:

A problem in one listener should not impact another listener
The order of listener calls is not defined by the JavaBean specification
There are two exceptions to this rule:

Constrained properties in a JavaBean talk to VetoableChangeListeners. The order of called listeners is not defined, but if any listener throws a PropertyVetoException, the remaining ones are not called.
If you write your own events (under the Beans model, same type of pattern as existing AWT events), their behavior is defined by you. You can implement and document that they have a certain order or will stop when any listeners throw an exception.
I strongly recommend against this as it's not normal behavior for event handling

Note that you cannot simply make the text property constrained. The problem is that the setText method for a constrained JTextField could throw a PropertyVetoException, which isn't declared in the setText method of JTextField.

There are really two things you can do here:

You could write a custom event for this, and implement/document that it calls listeners in the added order and that if any throw an exception, the others are not called.
Add a single focusLost listener that does the multiple validations in order, stopping if any fail.
I'd recommend the second approach, like the following:


JTextField f = new JTextField();
f.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {
try {
validation1();
validation2();
validation3();
}
catch(Exception e) {
// report the validation error
}
}
});

If any of the validations fail (throw an exception), the others are not processed.

Finally, Don't forget that a call to setText() can change the value of the field!. You should override setText() and include validation as well.


skyyoung 2001-10-31
  • 打赏
  • 举报
回复
to coaa(我吃多了) :
上排没有空,所以
gdsean 2001-10-31
  • 打赏
  • 举报
回复
我还有个url合法判别,如果输入的url是非法的,
那么就弹个对话框出来,你总不能把人家原来输入的
东西给去掉,换上一个自己合法的default值吧
不然对话框又会总是跳出来
加载更多回复(6)

62,614

社区成员

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

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