e618. Validating a JTextField When Permanently Losing the Focus
The when focus
2023-09-14 09:11:59 时间
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 |
相关文章
- There are inconsistent line endings in the 'xxx' script. Some are Mac OS X (UNIX) and some are Windows.问题解决
- the network bridge on device vmnet0 is not running解决方法
- [Angular] Lazy Load CSS at runtime with the Angular CLI
- Caused by: org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must end with the ';' delimiter.
- [Typescript] Tips: Create your own 'objectKeys' function using generics and the 'keyof' operator
- [MST] Create an Entry Form to Add Models to the State Tree
- [CSS] Change the auto-placement behaviour of grid items with grid-auto-flow
- how does SAP ui5 know the phone, tablet type, os type
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- 成功解决The following specifications were found to be incompatible with the existing python installation
- 成功解决MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found. To build using the
- 【K3s】第25篇 解决The connection to the server localhost:8080 was refused
- OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program.
- error when loading the sdk 发现了元素 d:skin 开头无效内容
- HDU4869:Turn the pokers(费马小定理+高速幂)
- e620. Activating a Keystroke When Any Component in the Window Has Focus
- 存在隐患 : 3 racks are required for the erasure coding policies: RS-6-3-1024k. The number of racks is on
- An attempt has been made to start a new process before the current process
- Docker学习问题记录:The container name "/zabbix-agent" is already in use by