JTextField: setInputVerifier(InputVerifier input) : JTextField « javax.swing « Java by API






JTextField: setInputVerifier(InputVerifier input)

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, 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(source, "Can't leave.", "Error Dialog",
              JOptionPane.ERROR_MESSAGE);
          return false;
        } else {
          return true;
        }
      }
    };
    nameTextField.setInputVerifier(verifier);

    
    frame.setSize(250, 100);
    frame.setVisible(true);
  }
}

           
         
  








Related examples in the same category

1.JTextField.LEFT
2.JTextField.LEADING
3.JTextField.TRAILING
4.new JTextField(String text)
5.new JTextField(String text, int columns)
6.JTextField: addActionListener(ActionListener s)
7.JTextField: addFocusListener(FocusListener l)
8.JTextField: addKeyListener(KeyListener l)
9.JTextField: copy()
10.JTextField: cut()
11.JTextField: getActions()
12.JTextField: getCaretPosition()
13.JTextField: getDocument()
14.JTextField: getHorizontalVisibility()
15.JTextField: getHorizontalVisibility() (2)
16.JTextField: getInputMap()
17.JTextField: getKeymap()
18.JTextField: getScrollOffset()
19.JTextField: paste()
20.JTextField: registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition)
21.JTextField: requestFocus()
22.JTextField: modelToView(int pos)
23.JTextField: read(Reader in, Object desc)
24.JTextField: setDocument(Document doc)
25.JTextField: setFocusAccelerator(char aKey)
26.JTextField: setHorizontalAlignment(int alignment)
27.JTextField: setPreferredSize(Dimension preferredSize)
28.JTextField: setNavigationFilter(NavigationFilter filter)
29.JTextField: setTransferHandler(TransferHandler newHandler)
30.JTextField: write(Writer out)