如何获得JComboBox自动建议

时间:2014-02-24 15:13:28

标签: java swing netbeans autosuggest suggestbox

朋友们,在这个项目中,我需要一个列表建议作为用户输入。我已经看了很多例子,并开始研究它,它有点工作。问题是,它只显示输入的第一个字母的建议,当输入第二个字母时,建议消失。 请帮我弄清楚原因。 编写我的代码,以便在按下右箭头键时自动完成列表中的第一个选项。

Vector<String> v = new Stack<String>();
public JTextField tx;
private boolean hide_flag = false;
tx = (JTextField)name.getEditor().getEditorComponent();
tx.addKeyListener(new KeyAdapter() {
        @Override
public void keyTyped(KeyEvent e){
EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                 String text = tx.getText().toUpperCase();
                 if(text.length()==0){
                 name.hidePopup();
                 setModel(new DefaultComboBoxModel(v),"");
                         }
                 else{
                     DefaultComboBoxModel m = getSuggestedModel(v,text);
                      if(m.getSize()==0){
                 name.hidePopup();
                      }
                      else{
                 setModel(m,text);
                  name.showPopup();
                      }
                 }

                }


            });
 }
        @Override
 public void keyPressed(KeyEvent evt){
 String text = tx.getText();
 int code = evt.getKeyCode();
 if(code == KeyEvent.VK_ENTER){
      if(!v.contains(text)){
                v.addElement(text);
          }
   hide_flag = true;
 }else if(code == KeyEvent.VK_ESCAPE){
 hide_flag = true ;
 }else if(code == KeyEvent.VK_RIGHT){
   for(int i=0; i< v.size();i++){
       String str = (String)v.elementAt(i);
        if(str.startsWith(text)){
          tx.setText(str);
            return;
        }}
 }
 }
  });

  }
 private void setModel(DefaultComboBoxModel Mdl, String str) {
                    name.setModel(Mdl);
                    tx.setText(str);
                }
 private DefaultComboBoxModel getSuggestedModel(List<String> list , String text){
     DefaultComboBoxModel m = new  DefaultComboBoxModel(); 
     for(String s : list){
         if(s.startsWith(text)){
       m.addElement(s);  
     }
     }
     return m;
 }

我希望自动完成能够在输入时建议单词。不仅仅是第一个字母。

1 个答案:

答案 0 :(得分:0)

  

“请给我一些例子”

不要重新发明轮子。已经有很多实现了。只需进行谷歌搜索。这是一对夫妇

SwingX

Maven依赖

    <dependency>
        <groupId>org.swinglabs</groupId>
        <artifactId>swingx</artifactId>
        <version>1.6.1</version>
    </dependency>

enter image description here

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

public class SwingXDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JComboBox comboBox;
                comboBox = new JComboBox(new Object[]{"Ester", "Jordi",
                    "Jordina", "Jorge", "Sergi"});
                AutoCompleteDecorator.decorate(comboBox);
                System.out.println("Is editable - "
                        + comboBox.isEditable() + ". Surprise!");
                JOptionPane.showMessageDialog(null, comboBox);
            }

        });
    }
}

JIDE

Maven依赖

    <dependency>
        <groupId>com.jidesoft</groupId>
        <artifactId>jide-oss</artifactId>
        <version>3.5.13</version>
    </dependency>

enter image description here

import com.jidesoft.swing.AutoCompletionComboBox;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class JIDEDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                AutoCompletionComboBox comboBox;
                Object[] elements = new Object[]{"Ester", "Jordi", "Jordina",
                    "Jorge", "Sergi"};
                comboBox = new AutoCompletionComboBox(elements);
                comboBox.setStrict(false);

                JOptionPane.showMessageDialog(null, comboBox);
            }
        });
    }
}