Listening to Text Components Events with an InputVerifier : JTextComponent « Swing « Java Tutorial






Do field-level validation of a JTextField. Before focus moves out of a text component, the verifier runs. If not valid, the verifier rejects the change and keeps input focus within the given component.

Listening to Text Components Events with an InputVerifier
import java.awt.BorderLayout;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class AddingActionCommandActionListenerSample {
  public static void main(String args[]) {
    final JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    InputVerifier verifier = new InputVerifier() {
      public boolean verify(JComponent input) {
        final JTextComponent source = (JTextComponent)input;
        String text = source.getText();
        if ((text.length() != 0) && !(text.equals("Exit"))) {
              JOptionPane.showMessageDialog (frame, "Can't leave.",
                "Error Dialog", JOptionPane.ERROR_MESSAGE);
          return false;
        } else {
          return true;
        }
      }
    };
    textField.setInputVerifier(verifier);
    
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}








14.14.JTextComponent
14.14.1.JTextComponent: the parent class for all the components used as textual views.
14.14.2.Drawing in the Background of a ComponentDrawing in the Background of a Component
14.14.3.Loading and Saving ContentLoading and Saving Content
14.14.4.Accessing the System ClipboardAccessing the System Clipboard
14.14.5.Default Editor Kit: cutActionDefault Editor Kit: cutAction
14.14.6.Register Keyboard action: registerKeyboardActionRegister Keyboard action: registerKeyboardAction
14.14.7.CaretListener Interface and CaretEvent ClassCaretListener Interface and CaretEvent Class
14.14.8.Restricting Caret Movement: NavigationFilterRestricting Caret Movement: NavigationFilter
14.14.9.Remove Key action from Text componentRemove Key action from Text component
14.14.10.Listening to Text Components Events with an ActionListenerListening to Text Components Events with an ActionListener
14.14.11.Listening to JTextField Events with an KeyListenerListening to JTextField Events with an KeyListener
14.14.12.Share Document
14.14.13.Listening to Text Components Events with an InputVerifierListening to Text Components Events with an InputVerifier
14.14.14.TextAction Name Constants
14.14.15.Using Text Component ActionsUsing Text Component Actions
14.14.16.Listening to Text Components Events with a DocumentListenerListening to Text Components Events with a DocumentListener
14.14.17.Text Component PrintingText Component Printing
14.14.18.GIF Writer
14.14.19.Text component supports both cut, copy and paste (using the DefaultEditorKit's built-in actions) and drag and drop
14.14.20.Overriding the Default Action of a JTextComponent
14.14.21.Modifying Text in a JTextComponent: Insert some text at the beginning
14.14.22.Modifying Text in a JTextComponent: Insert some text after the 5th character
14.14.23.Modifying Text in a JTextComponent: Append some text
14.14.24.Modifying Text in a JTextComponent: Delete the first 5 characters
14.14.25.Modifying Text in a JTextComponent: Replace the first 3 characters with some text
14.14.26.Limiting the Capacity of a JTextComponent
14.14.27.Filter all editing operations on a text component
14.14.28.Enabling Text-Dragging on a JTextComponent
14.14.29.Enumerating All the Views in a JTextComponent
14.14.30.Sharing a Document Between JTextComponents
14.14.31.Highlight a word in JTextComponent
14.14.32.Remove Highlighting in a JTextComponent
14.14.33.Listening for Caret Movement Events in a JTextComponent
14.14.34.Listening for Editing Changes in a JTextComponent
14.14.35.Moving the Caret of a JTextComponent
14.14.36.Setting the Blink Rate of a JTextComponent's Caret
14.14.37.Using the Selection of a JTextComponent
14.14.38.Better way to set the selection
14.14.39.Set the color behind the selected text
14.14.40.Set the caret color
14.14.41.Document and DocumentFilter