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

 一、实现效果

二、代码

 

Java代码 复制代码  收藏代码
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import java.util.*;   
  4.      
  5. import javax.swing.*;   
  6. import javax.swing.event.*;   
  7.   
  8.  public class Test {   
  9.   
  10.     public static void main(String[] args) throws Exception {   
  11.          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());   
  12.         JFrame frame = new JFrame();   
  13.           frame.setTitle("Auto Completion Test");   
  14.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  15.          frame.setBounds(200200500400);   
  16.   
  17.          ArrayList<String> items = new ArrayList<String>();   
  18.          Locale[] locales = Locale.getAvailableLocales();   
  19.          for (int i = 0; i < locales.length; i++) {   
  20.              String item = locales[i].getDisplayName();     
  21.              System.out.println(item);   
  22.              items.add(item);   
  23.          }   
  24.          JTextField txtInput = new JTextField();   
  25.          setupAutoComplete(txtInput, items);   
  26.          txtInput.setColumns(30);   
  27.          frame.getContentPane().setLayout(new FlowLayout());   
  28.          frame.getContentPane().add(txtInput, BorderLayout.NORTH);      
  29.          frame.setVisible(true);   
  30.      }   
  31.   
  32.      private static boolean isAdjusting(JComboBox cbInput) {   
  33.          if (cbInput.getClientProperty("is_adjusting"instanceof Boolean) {   
  34.              return (Boolean) cbInput.getClientProperty("is_adjusting");   
  35.          }   
  36.          return false;   
  37.      }   
  38.   
  39.      private static void setAdjusting(JComboBox cbInput, boolean adjusting) {   
  40.          cbInput.putClientProperty("is_adjusting", adjusting);   
  41.      }   
  42.   
  43.      public static void setupAutoComplete(final JTextField txtInput, final ArrayList<String> items) {   
  44.          final DefaultComboBoxModel model = new DefaultComboBoxModel();   
  45.          final JComboBox cbInput = new JComboBox(model) {   
  46.              public Dimension getPreferredSize() {   
  47.                  return new Dimension(super.getPreferredSize().width, 0);   
  48.              }   
  49.          };   
  50.          setAdjusting(cbInput, false);   
  51.          for (String item : items) {   
  52.              model.addElement(item);   
  53.          }   
  54.          cbInput.setSelectedItem(null);   
  55.          cbInput.addActionListener(new ActionListener() {   
  56.              @Override  
  57.              public void actionPerformed(ActionEvent e) {   
  58.                  if (!isAdjusting(cbInput)) {   
  59.                      if (cbInput.getSelectedItem() != null) {   
  60.                          txtInput.setText(cbInput.getSelectedItem().toString());   
  61.                      }   
  62.                  }   
  63.              }   
  64.          });   
  65.   
  66.          txtInput.addKeyListener(new KeyAdapter() {   
  67.   
  68.              @Override  
  69.              public void keyPressed(KeyEvent e) {   
  70.                  setAdjusting(cbInput, true);   
  71.                  if (e.getKeyCode() == KeyEvent.VK_SPACE) {   
  72.                      if (cbInput.isPopupVisible()) {   
  73.                          e.setKeyCode(KeyEvent.VK_ENTER);   
  74.                      }   
  75.                  }   
  76.                  if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {   
  77.                      e.setSource(cbInput);   
  78.                      cbInput.dispatchEvent(e);   
  79.                      if (e.getKeyCode() == KeyEvent.VK_ENTER) {   
  80.                          txtInput.setText(cbInput.getSelectedItem().toString());   
  81.                          cbInput.setPopupVisible(false);   
  82.                      }   
  83.                  }   
  84.                  if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {   
  85.                      cbInput.setPopupVisible(false);   
  86.                  }   
  87.                  setAdjusting(cbInput, false);   
  88.              }   
  89.          });   
  90.          txtInput.getDocument().addDocumentListener(new DocumentListener() {   
  91.              public void insertUpdate(DocumentEvent e) {   
  92.                  updateList();   
  93.              }   
  94.   
  95.              public void removeUpdate(DocumentEvent e) {   
  96.                  updateList();   
  97.              }   
  98.   
  99.              public void changedUpdate(DocumentEvent e) {   
  100.                  updateList();   
  101.              }   
  102.   
  103.              private void updateList() {   
  104.                  setAdjusting(cbInput, true);   
  105.                  model.removeAllElements();   
  106.                  String input = txtInput.getText();   
  107.                  if (!input.isEmpty()) {   
  108.                      for (String item : items) {   
  109.                          if (item.toLowerCase().startsWith(input.toLowerCase())) {   
  110.                              model.addElement(item);   
  111.                          }   
  112.                      }   
  113.                  }   
  114.                  cbInput.setPopupVisible(model.getSize() > 0);   
  115.                  setAdjusting(cbInput, false);   
  116.              }   
  117.          });   
  118.          txtInput.setLayout(new BorderLayout());   
  119.          txtInput.add(cbInput, BorderLayout.SOUTH);   
  120.      }   
  121.  }  
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);
     }
 }

 

你可能感兴趣的:(java,动态,信息)