e618. Validating a JTextField When Permanently Losing the Focus
This example demonstrates a text field that validates its contents when it receives a permanent focus-lost event. If the contents are invalid, it displays a modal dialog with an error message and regains the focus.
JTextField component = new JTextField(10);
component.addFocusListener(new MyFocusListener()); public class MyFocusListener extends FocusAdapter {
boolean showingDialog = false; public void focusGained(FocusEvent evt) {
final JTextComponent c = (JTextComponent)evt.getSource();
String s = c.getText(); // Position the caret at the 1st non-digit character
for (int i=0; i<s.length(); i++) {
// Ensure validity
if (!Character.isDigit(s.charAt(i))) {
c.setSelectionStart(i);
c.setSelectionEnd(i);
break;
}
}
}
public void focusLost(FocusEvent evt) {
final JTextComponent c = (JTextComponent)evt.getSource();
String s = c.getText(); if (evt.isTemporary()) {
return;
}
for (int i=0; i<s.length(); i++) {
// Ensure validity
if (!Character.isDigit(s.charAt(i))) {
// Find top-level window
Component par = c;
while (par.getParent() != null) {
par = par.getParent();
}
final Frame frame = (Frame)par; // Create and display an error message
JOptionPane optionPane = new JOptionPane("The value must only contain digits",
JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
optionPane.createDialog(frame, null).show(); // Regain the focus
c.requestFocus();
break;
}
}
}
}
Related Examples |
e618. Validating a JTextField When Permanently Losing the Focus的更多相关文章
- stl_alloc.h
/* * Copyright (c) 1996-1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, ...
- 《STL源代码剖析》---stl_alloc.h阅读笔记
这一节是讲空间的配置与释放,但不涉及对象的构造和析构,仅仅是解说对象构造前空前的申请以及对象析构后空间怎么释放. SGI版本号的STL对空间的的申请和释放做了例如以下考虑: 1.向堆申请空间 2.考虑 ...
- STL stl_alloc.h
# // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: http://blog.csdn.net/mdl13412 # # // ...
- SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis
catalog . introduction . sqlchop sourcecode analysis . SQLWall(Druid) . PHP Syntax Parser . SQL Pars ...
- 所有CM_消息的说明
这些CM消息,居然在Delphi的帮助里是没有任何说明的,真是昏倒.意外在高手的书里找到了所有说明,说明如下: Message Constant Value Description cm_Base $ ...
- puppeteer(五)chrome启动参数列表API
List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...
- CEF 支持的命令行参数
参考:https://peter.sh/experiments/chromium-command-line-switches/ List of Chromium Command Line Switch ...
- e617. Determining the Opposite Component of a Focus Event
The opposite component is the other component affected in a focus event. Specifically, in a focus-lo ...
- C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字
最近写了个小程序,用于将固态硬盘的写入量等信息显示在任务栏,最开始使用Windows API也可以实现,但是当任务栏托盘增加的时候,会被遮盖,最终采用了DeskBand来实现,填了很多坑. 参考的Gi ...
随机推荐
- javascript的toString深入探究
toString()方法是所有对象都有的一个方法,无论是字符串,数组,对象,都可以调用这个方法,但是,事实上,他们调用的并不是同一个函数哦! 看下面的代码: var str = '123'; cons ...
- Let's Encrypt申请免费SSL证书
1.https的作用 安全,防止网站被劫持,数据被修改 2.Let's Encrypt是什么 Let's Encrypt是一个证书授权机构(CA),可以从Let's Encrypt获得网站域名的免费证 ...
- 关键词抽取:pagerank,textrank
摘抄自微信公众号:AI学习与实践 TextRank,它利用图模型来提取文章中的关键词.由 Google 著名的网页排序算法 PageRank 改编而来的算法. PageRank PageRank 是一 ...
- 【delphi】delphi操作sqlite3
SQLite SQLite是一个老牌的轻量级别的本地文件数据库,完全免费且开源,不需要安装,无须任何配置,当然,这样管理功能就不是很强大了,但是它的主要应用也是在本地数据库,可以说是最简单好用的嵌入式 ...
- 比较正确的 iPhone7/7+ 的进入DFU的方法是这样的
正确的.没有歧义的.在WIndows7系统下进入DFU并刷机的方法是: 1)PC端打开iTunes,数据线连接iPhone7与PC: 2)iPhone7关机: 3)同时按下电源键和音量减键,LOGO会 ...
- css超出一行添加省略号属性:text-overflow和white-space
通过使用text-overflow和white-space属性来使文本在一行内显示,超出则加省略号,添加如下html代码: <p>前端开发博客专注前端开发和技术分享,如果描述超过100像素 ...
- type、object和class的关系
- kill-9导致weblogic无法启动
转载自:http://blog.csdn.net/lykangjia/article/details/17486127?rsv_upd=1 今天单位系统遇到一个问题: Resolve Weblogic ...
- decode和encode
作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- [转]table中设置tr行间距
原文地址:https://blog.csdn.net/itmyhome1990/article/details/50475616 CSS border-collapse 属性设置表格的边框是否被合并为 ...