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 ...
随机推荐
- PHP网站环境搭配: Apache Http+PHP+Mysql
Apache Http+PHP+Mysql 环境搭配 1. 先下载上述三个软件 都要下载对应系统的软件,mysql还可以再下载navicat for mysql. 2. 安装Apache Http ...
- python List使用
1.enumerate 用在遍历中,返回下标和数据 name_arr = ["shijingjing", "renjiangfeng", "anqi& ...
- Window 分布式学习-好文收藏
概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Linux和Mac上运行.那么大家(开发者和企业)为 ...
- Asp.Net时间方法大全
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(- Convert.ToInt32(dt.DayOfWeek.To ...
- form的验证包括手机号邮箱等等
$(function(){ var checkedByVerifyCode = false; var checkMobieCode = false; var checkedMobil ...
- Django-管理站点重写admin模板
参考链接:https://blog.csdn.net/u013378306/article/details/79023242 使用Django的admin管理工具,可以快速的构建自己的管理平台,使用D ...
- IIS7 MVC 403 禁止访问:访问被拒绝
- pymongo 使用测试
>>> import pymongo >>> uri = "mongodb://recall:123456@oceanic.mongohq.com:100 ...
- vs2010中TargetName与链接器输出名不一致
当出现解决方案生成正确,但是无法执行.exe文件,即系统无法找到指定文件时,提示有:MSB8012: TargetName(,,,) 与 Linker 的 OutputFile 属性(,,,)不匹配. ...
- Java NIO 系列教程 <转>
Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(B ...