java自动补全_Java实现自动补全提示的文本框

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.ArrayList;

import javax.swing.JList;

import javax.swing.JTextField;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

public class AutoCompletionField extends JTextField implements DocumentListener, MouseListener, ListSelectionListener, ActionListener, KeyListener {

private static int DEFAULT_PREFERRED_HEIGHT = 100;

private ListPopup popup;

private int preferredHeight = DEFAULT_PREFERRED_HEIGHT;

private CompletionFilter filter;

public void setFilter(CompletionFilter f) {

filter = f;

}

public AutoCompletionField() {

popup = new ListPopup();

getDocument().addDocumentListener(this);

addMouseListener(this);

popup.addListSelectionListener(this);

addActionListener(this);

addKeyListener(this);

}

public void setPopupPreferredHeight(int h) {

preferredHeight = h;

}

private boolean isListChange(ArrayList array) {

if (array.size() != popup.getItemCount()) {

return true;

}

for (int i = 0; i < array.size(); i++) {

if (!array.get(i).equals(popup.getItem(i))) {

return true;

}

}

return false;

}

private void textChanged() {

if ("".equals(this.getText()))

popup.setVisible(false);

else {

if (!popup.isVisible()) {

showPopup();

requestFocus();

}

if (filter != null) {

ArrayList array = filter.filter(getText());

changeList(array);

}

}

}

private void showPopup() {

popup.setPopupSize(getWidth(), preferredHeight);

popup.show(this, 0, getHeight()-1);

}

private void changeList(ArrayList array) {

if (array.size() == 0) {

if (popup.isVisible()) {

popup.setVisible(false);

}

} else {

if (!popup.isVisible()) {

showPopup();

}

}

if (isListChange(array)&&array.size()!=0) {

popup.setList(array);

}

}

public void insertUpdate(DocumentEvent e) {

textChanged();

}

public void removeUpdate(DocumentEvent e) {

textChanged();

}

public void changedUpdate(DocumentEvent e) {

textChanged();

}

public void mouseClicked(MouseEvent e) {

if(e.getClickCount()>1 && !popup.isVisible())

textChanged();

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void valueChanged(ListSelectionEvent e) {

JList list=(JList)e.getSource();

String text=list.getSelectedValue().toString();

setText(text);

popup.setVisible(false);

}

public void actionPerformed(ActionEvent e) {

if(popup.isVisible()){

Object o=popup.getSelectedValue();

if(o!=null)

setText(o.toString());

popup.setVisible(false);

}

this.selectAll();

this.requestFocus();

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_DOWN){

if(popup.isVisible()){

if(!popup.isSelected())

popup.setSelectedIndex(0);

else

popup.setSelectedIndex(popup.getSelectedIndex()+1);

}

}else if(e.getKeyCode()==KeyEvent.VK_UP){

if(popup.isVisible()){

if(!popup.isSelected())

popup.setLastOneSelected();

else

popup.setSelectedIndex(popup.getSelectedIndex()-1);

}

}else if(e.getKeyCode()==KeyEvent.VK_PAGE_DOWN){

if(popup.isVisible()){

if(!popup.isSelected())

popup.setSelectedIndex(0);

else

popup.setSelectedIndex(popup.getSelectedIndex()+5);

}

}else if(e.getKeyCode()==KeyEvent.VK_PAGE_UP){

if(popup.isVisible()){

if(!popup.isSelected())

popup.setLastOneSelected();

else

popup.setSelectedIndex(popup.getSelectedIndex()-5);

}

}

}

public void keyReleased(KeyEvent e) {

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值