Create an autofill JTextField by looking ahead in Java

Description

The following code shows how to create an autofill JTextField by looking ahead.

Example


//w  w  w  .  j av a2s  .  c om
/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Main {
  public static void main(String[] args) {

    JFrame f = new JFrame("Text Lookahead");

    StringArrayLookAhead lookAhead = new StringArrayLookAhead(values);
    LookAheadTextField tf = new LookAheadTextField(20, lookAhead);

    f.getContentPane().add(tf, "Center");
    f.pack();
    f.setVisible(true);
  }

  // The possible look-ahead values
  public static String[] values = new String[] { "aback", "abacus",
      "abandon", "abashed", "abate", "abdomen", "abide", "ability",
      "baby", "back", "backache", "backgammon" };
}

class LookAheadTextField extends JTextField {
  public LookAheadTextField() {
    this(0, null);
  }

  public LookAheadTextField(int columns) {
    this(columns, null);
  }

  public LookAheadTextField(int columns, TextLookAhead lookAhead) {
    super(columns);
    setLookAhead(lookAhead);
    addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        // Remove any existing selection
        setCaretPosition(getDocument().getLength());
      }
    });
    addFocusListener(new FocusListener() {
      public void focusGained(FocusEvent evt) {
      }

      public void focusLost(FocusEvent evt) {
        if (evt.isTemporary() == false) {
          // Remove any existing selection
          setCaretPosition(getDocument().getLength());
        }
      }
    });
  }

  public void setLookAhead(TextLookAhead lookAhead) {
    this.lookAhead = lookAhead;
  }

  public TextLookAhead getLookAhead() {
    return lookAhead;
  }

  public void replaceSelection(String content) {
    super.replaceSelection(content);

    if (isEditable() == false || isEnabled() == false) {
      return;
    }

    Document doc = getDocument();
    if (doc != null && lookAhead != null) {
      try {
        String oldContent = doc.getText(0, doc.getLength());
        String newContent = lookAhead.doLookAhead(oldContent);
        if (newContent != null) {
          // Substitute the new content
          setText(newContent);

          // Highlight the added text
          setCaretPosition(newContent.length());
          moveCaretPosition(oldContent.length());
        }
      } catch (BadLocationException e) {
        // Won't happen
      }
    }
  }

  protected TextLookAhead lookAhead;

  // The TextLookAhead interface
  public interface TextLookAhead {
    public String doLookAhead(String key);
  }
}

class StringArrayLookAhead implements LookAheadTextField.TextLookAhead {
  public StringArrayLookAhead() {
    values = new String[0];
  }

  public StringArrayLookAhead(String[] values) {
    this.values = values;
  }

  public void setValues(String[] values) {
    this.values = values;
  }

  public String[] getValues() {
    return values;
  }

  public String doLookAhead(String key) {
    int length = values.length;

    // Look for a string that starts with the key
    for (int i = 0; i < length; i++) {
      if (values[i].startsWith(key) == true) {
        return values[i];
      }
    }

    // No match found - return null
    return null;
  }

  protected String[] values;
}

The code above generates the following result.

Create an autofill JTextField by looking ahead in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer