extends PlainDocument to create alpha and numeric value based field field : Document Event « Swing JFC « Java






extends PlainDocument to create alpha and numeric value based field field

    

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

class JTextFieldFilter extends PlainDocument {
  public static final String ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  public static final String NUMERIC = "0123456789";
  public static final String ALPHA_NUMERIC = ALPHA + NUMERIC;

  protected String acceptedChars = null;

  protected boolean negativeAccepted = false;

  public JTextFieldFilter() {
    this(ALPHA_NUMERIC);
  }

  public JTextFieldFilter(String acceptedchars) {
    acceptedChars = acceptedchars;
  }

  public void setNegativeAccepted(boolean negativeaccepted) {
    if (acceptedChars.equals(ALPHA_NUMERIC)) {
      negativeAccepted = negativeaccepted;
      acceptedChars += "-";
    }
  }

  public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
    if (str == null)
      return;
    for (int i = 0; i < str.length(); i++) {
      if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
        return;
    }

    if (negativeAccepted) {
      if (str.indexOf(".") != -1) {
        if (getText(0, getLength()).indexOf(".") != -1) {
          return;
        }
      }
    }

    if (negativeAccepted && str.indexOf("-") != -1) {
      if (str.indexOf("-") != 0 || offset != 0) {
        return;
      }
    }

    super.insertString(offset, str, attr);
  }
}

public class Main extends JFrame{
  public static void main(String[] argv) throws Exception {
    new Main();
  }

  public Main() {
    JTextField tf1c;
    JLabel l1c;

    setLayout(new FlowLayout());
    l1c = new JLabel("only float(can be negative)");
    tf1c = new JTextField(10);
    getContentPane().add(l1c);
    getContentPane().add(tf1c);
    JTextFieldFilter jtff = new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC);
    jtff.setNegativeAccepted(true);
    tf1c.setDocument(jtff);
    
    setSize(300,300);
    setVisible(true);
  }
}   

   
    
    
    
  








Related examples in the same category

1.DocumentFilter that maps lowercase letters to uppercaseDocumentFilter that maps lowercase letters to uppercase
2.An extension of PlainDocument that restricts the length of its contentAn extension of PlainDocument that restricts the length of its content
3.Document Event DemoDocument Event Demo
4.React to the document update insert changed eventReact to the document update insert changed event
5.HTMLDocument: Document Iterator Example
6.DocumentListener DemoDocumentListener Demo
7.extends PlainDocument to create a float value type text field
8.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
9.Document ElementIterator DemoDocument ElementIterator Demo
10.Format JTextField's text to uppercase
11.Overriding a Few Default Typed Key Bindings in a JTextComponent
12.Overriding - key
13.Override $ key
14.Overriding space key
15.Disable a character so that no action is invoked.
16.Bind a keystroke to shift-space
17.Prevent the space from being inserted when shift-space is pressed.
18.Subclass InputVerifier
19.extends PlainDocument
20.This Document restricts the size of the contained plain text to the given number of characters.
21.A frame with three text fields to set the background color.