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 ...
随机推荐
- python-enumerate方法
enumerate方法用于循环list或tuple,循环的时候可以得到索引值和当前索引的对象: letters = ['a', 'b', 'c', 'd', 'e'] for i, letter in ...
- C#之Console
Console.Write 表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine 表示向控制台写入字符串后换行.Console.Read 表示从控制 ...
- Tornado使用-队列Queue
1.tornado队列的特点 和python标准队列queue相比,tornado的队列Queue支持异步 2.Queue常用方法 Queue.get() 会暂停,直到queue中有元素 Queue. ...
- lua的模块加载require
加载指定的模块.首先函数会在 package.loaded 这个表中查看是否已经加载 了 modname 这个模块.如果是,那么 require 会返回保存在 package.loaded[modna ...
- Eclipse - Mac Os Default JRE missing
转:http://stackoverflow.com/questions/1736993/eclipse-mac-os-default-jre-missing 1) Follow Joshua's a ...
- [Java]随记--HttpClient发送put请求
http://blog.csdn.net/u010989191/article/details/52852155 ******************************************* ...
- AIX上打包排除某些文件/文件夹
Syntax X/Open Standards: tar {-c|-r|-t|-u|-x} [-B] [ -d ] [ -E ] [ -F ] [-h ] [ -i ] [ -l ] [ -m ] [ ...
- zoj 3761(并查集+搜索)
题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...
- C#学习笔记(5)——大项目查找
说明(2017-5-27 16:34:39): 1. 注意事项: (0)设计窗体,添加一个dgv,添加5个列名,修改名字和绑定数据. (1)添加引用,system.data.sqlclient (2) ...
- 深入理解IoC和DI
本文章转载自: https://segmentfault.com/a/1190000005602011 最近在研究php的lumen框架和phalcon框架,这两个框架的底层架构都用到了IoC,DI, ...