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 ...
随机推荐
- Sql Server 2000/2008 用Sql导出数据表结构(包括注释)到Excel
--Sql Server 2000 declare @id int, ), ) declare cur_1 cursor for select id,name from sysobjects wher ...
- [Windows Azure] Adding Sign-On to Your Web Application Using Windows Azure AD
Adding Sign-On to Your Web Application Using Windows Azure AD 14 out of 19 rated this helpful - Rate ...
- Android 编程下通过 zipalign 对 APK 文件进行优化
zipalign zipalign is an archive alignment tool that provides important optimization to Android appli ...
- 【ARM】AD转换器
A/D转换器 A/D转换器,又称模/数转换器,顾名思义,就是把模拟信号数字化. 由于系统的实际处理对象往往都是一些模拟量(如温度.压力.位移.图像等),要使计算机或数字仪表能识别和处理这些信号,必须首 ...
- 【Cmd】那些年,我们迷恋的cmd命令(二)
那些年,我们迷恋的命令(一) 那些年,我们迷恋的命令(二) Linux系统下基本命令 Linux系统下基本命令: 要区分大小写 uname 显示版本信息(同win2K的 ver) dir 显示当前目录 ...
- PostgreSQL获取table名,字段名
PostgreSQL获取数据库中所有table名: SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablena ...
- jetty debug修改 java static 静态变量值不会生效
在jetty debug模式下修改static静态变量值不会重新Load 因为jetty是嵌入式web容器,static静态变量是全局的,如果想生效,就必须重启jetty 在热部署的时候tomcat会 ...
- pandas入门——loc与iloc函数
oc与iloc函数 loc函数 import pandas as pd import numpy # 导入数据 df = pd.read_csv(filepath_or_buffer="D: ...
- docker探索-使用docker service管理swarm(十一 )
本文转自:https://www.cnblogs.com/atuotuo/p/6265541.html 1.创建一个 Docker service $ docker service create -- ...
- How Not to Crash #2: Mutation Exceptions 可变异常(不要枚举可变的集合)
How Not to Crash #2: Mutation Exceptions 可变异常html, body {overflow-x: initial !important;}html { font ...