import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
public class Test4 extends JFrame {
private static final long serialVersionUID = 1L;
public void TestTestField(){
this.setLayout(new BorderLayout());
this.add(new CacheTextField(),BorderLayout.SOUTH);
this.add(new JLabel("pase enter ,data save in the cache, reinput,you can see point out"));
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Test4 test = new Test4();
test.TestTestField();
}
}
class CacheTextField extends JTextField implements KeyListener{
private static final long serialVersionUID = 1L;
HashSet<String> cache = new HashSet<String>();
CacheTextField(){
this.addKeyListener(this);
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() >= KeyEvent.VK_LEFT && e.getKeyCode() <= KeyEvent.VK_DOWN){
return;
}
if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE){
return;
}
if(e.getKeyCode()==KeyEvent.VK_ENTER){
cache.add(getText().trim());
setText("");
}else{
String str = null;
try {
str = this.getText(0, this.getCaretPosition());
} catch (BadLocationException e2) {
e2.printStackTrace();
}
for(String txt : cache){
if(txt.length()<=str.length()){
continue;
}
if(txt.substring(0, str.length()).equals(str)){
int i = this.getCaretPosition();
this.setText(this.getText()+txt.substring(str.length()));
this.setCaretPosition(i);
this.select(i, this.getText().length());
return;
}
}
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}