The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > Focus  [16 examples]

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
e606. Determining Which Component or Window Has the Focus
e607. Preventing a Component from Gaining the Focus
e608. Preventing a Window from Gaining the Focus
e609. Listening to All Focus Changes Between Components in an Application
e610. Setting Focus Traversal Keys in a Component
e611. Setting Focus Traversal Keys for the Entire Application
e612. Moving the Focus to the Next or Previous Focusable Component
e613. Modifying the Focus Traversal Order
e614. Setting the Initial Focused Component in a Window
e615. Finding the Next Focusable Component
e616. Determining If a Focus Lost Is Temporary or Permanent
e617. Determining the Opposite Component of a Focus Event
e619. Removing the Focus from the Application
e620. Activating a Keystroke When Any Component in the Window Has Focus
e621. Activating a Keystroke When Any Child Component Has Focus

See also: Colors    Components    Containers    Cursors    Drawing    Events    Frames    GridBagLayout    Images    Shapes    Simulating Events    Text    The Screen   


© 2002 Addison-Wesley.