【java】JTextField与JComboBox结合动态匹配输入信息

一、实现效果

二、代码

 

import java.awt.*;
import java.awt.event.*;
import java.util.*;
  
import javax.swing.*;
import javax.swing.event.*;

 public class Test {

    public static void main(String[] args) throws Exception {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFrame frame = new JFrame();
          frame.setTitle("Auto Completion Test");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setBounds(200, 200, 500, 400);

         ArrayList<String> items = new ArrayList<String>();
         Locale[] locales = Locale.getAvailableLocales();
         for (int i = 0; i < locales.length; i++) {
             String item = locales[i].getDisplayName();  
             System.out.println(item);
             items.add(item);
         }
         JTextField txtInput = new JTextField();
         setupAutoComplete(txtInput, items);
         txtInput.setColumns(30);
         frame.getContentPane().setLayout(new FlowLayout());
         frame.getContentPane().add(txtInput, BorderLayout.NORTH);   
         frame.setVisible(true);
     }

     private static boolean isAdjusting(JComboBox cbInput) {
         if (cbInput.getClientProperty("is_adjusting") instanceof Boolean) {
             return (Boolean) cbInput.getClientProperty("is_adjusting");
         }
         return false;
     }

     private static void setAdjusting(JComboBox cbInput, boolean adjusting) {
         cbInput.putClientProperty("is_adjusting", adjusting);
     }

     public static void setupAutoComplete(final JTextField txtInput, final ArrayList<String> items) {
         final DefaultComboBoxModel model = new DefaultComboBoxModel();
         final JComboBox cbInput = new JComboBox(model) {
             public Dimension getPreferredSize() {
                 return new Dimension(super.getPreferredSize().width, 0);
             }
         };
         setAdjusting(cbInput, false);
         for (String item : items) {
             model.addElement(item);
         }
         cbInput.setSelectedItem(null);
         cbInput.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if (!isAdjusting(cbInput)) {
                     if (cbInput.getSelectedItem() != null) {
                         txtInput.setText(cbInput.getSelectedItem().toString());
                     }
                 }
             }
         });

         txtInput.addKeyListener(new KeyAdapter() {

             @Override
             public void keyPressed(KeyEvent e) {
                 setAdjusting(cbInput, true);
                 if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                     if (cbInput.isPopupVisible()) {
                         e.setKeyCode(KeyEvent.VK_ENTER);
                     }
                 }
                 if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
                     e.setSource(cbInput);
                     cbInput.dispatchEvent(e);
                     if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                         txtInput.setText(cbInput.getSelectedItem().toString());
                         cbInput.setPopupVisible(false);
                     }
                 }
                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                     cbInput.setPopupVisible(false);
                 }
                 setAdjusting(cbInput, false);
             }
         });
         txtInput.getDocument().addDocumentListener(new DocumentListener() {
             public void insertUpdate(DocumentEvent e) {
                 updateList();
             }

             public void removeUpdate(DocumentEvent e) {
                 updateList();
             }

             public void changedUpdate(DocumentEvent e) {
                 updateList();
             }

             private void updateList() {
                 setAdjusting(cbInput, true);
                 model.removeAllElements();
                 String input = txtInput.getText();
                 if (!input.isEmpty()) {
                     for (String item : items) {
                         if (item.toLowerCase().startsWith(input.toLowerCase())) {
                             model.addElement(item);
                         }
                     }
                 }
                 cbInput.setPopupVisible(model.getSize() > 0);
                 setAdjusting(cbInput, false);
             }
         });
         txtInput.setLayout(new BorderLayout());
         txtInput.add(cbInput, BorderLayout.SOUTH);
     }
 }


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值