关闭

swing实现自动完成(智能匹配)功能--紫枫凝潇烟

1538人阅读 评论(0) 收藏 举报

近日完成了一个较复杂的JTree结构,并提供搜索功能,想着在搜索时再加个智能匹配的功能应该能提高用户体验,于是先完成了这个示例 ,效果就不贴图了,就是类似google的suggest那样的智能匹配功能,代码如下

 

package swingdemo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;


public class IntelligentMatch extends JFrame{


    
private JTextField keywordField;

    
private JPopupMenu popupMenu;
    
private JList cacheKeywordList;
    
private Set<String> cacheKeyword = new HashSet<String>();
    
private Set<String> matchResultSet = new HashSet<String>();
    
private ResultListModel resultListModel;
    
private ListListener listListener = new ListListener();
    
    
public IntelligentMatch(){
        
super();
        init();
        
    }

    
    
public void init(){
        keywordField 
= new JTextField(22);
        Listener listener 
= new Listener();
        KeyDisposer keyListener 
= new KeyDisposer();
        keywordField.registerKeyboardAction(listener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 
0false),JComponent.WHEN_FOCUSED);
        
        JButton searchButton 
= new JButton("Search");
        searchButton.addMouseListener(listener);
        resultListModel 
= new ResultListModel();
        cacheKeywordList 
= new JList(resultListModel);
        cacheKeywordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cacheKeywordList.registerKeyboardAction(listListener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 
0false),JComponent.WHEN_FOCUSED);
        JComponent tipFrame 
= new JScrollPane(cacheKeywordList);
        tipFrame.setPreferredSize(
new Dimension(240,100));
        System.out.println(keywordField.getWidth());
        popupMenu 
= new JPopupMenu();
        popupMenu.add(tipFrame);
        keywordField.addKeyListener(keyListener);
        
        Container pane  
= getContentPane();
        pane.setLayout(
new BorderLayout());
        JPanel searchPanel 
= new JPanel();
        searchPanel.add(keywordField);
        searchPanel.add(searchButton);
        pane.add(searchPanel,BorderLayout.NORTH);
        JTable table 
= new JTable();
        pane.add(table,BorderLayout.CENTER);
        
        setSize(
500,300);
        setVisible(
true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    
    
private void doSearch(){
        String keyWord 
= keywordField.getText().trim();
        
if(!keyWord.equals("")){
            cacheKeyword.add(keyWord);
        }

        keywordField.setText(
"");
        
// do search .....
        new Thread(new Runnable(){

            
public void run() {
                
// searching...
            }

            
        }
).start();
    }

    
    
public void showMatch(){
        String keyWord 
= keywordField.getText().trim();
        
if(keyWord.equals("")){
            popupMenu.setVisible(
false);
            
return;
        }

        Set
<String> matchResult = getMatch(keyWord);
        
if(matchResult==null || matchResult.size()<1){
            popupMenu.setVisible(
false);
            
return;
        }

        resultListModel.removeAllElements();
        
for(String result:matchResult)
            resultListModel.addElement(result);
        popupMenu.show(keywordField,
0, keywordField.getHeight());
        cacheKeywordList.setSelectedIndex(
0);
        keywordField.requestFocus();
    }

    
    
private Set<String> getMatch(String content){
        Set
<String> tempSet = new HashSet<String>();
        
for(String temp:cacheKeyword)
            
if(temp.indexOf(content)!=-1)
                tempSet.add(temp);
        
return tempSet;
    }

    

    
public static void main(String[] args) {
        
new IntelligentMatch();
    }


    
    
    
    
/**inner class for answering event*/
    
private class KeyDisposer extends KeyAdapter{
        
public void keyReleased(KeyEvent e){
            
if(e.getKeyCode()==27){
                popupMenu.setVisible(
false);
            }
else if(e.getKeyCode()==40){
                popupMenu.setVisible(
true);
                cacheKeywordList.requestFocus();
                
if(resultListModel.size()>=2)
                    cacheKeywordList.setSelectedIndex(
1);
            }
else if(e.getKeyCode()==38){
                popupMenu.setVisible(
true);
                cacheKeywordList.requestFocus();
                cacheKeywordList.setSelectedIndex(resultListModel.size()
-1);
            }
else if(e.getKeyCode()==KeyEvent.VK_ENTER){
                
            }
else {
                showMatch();
            }

        }

    }

    
class Listener extends MouseAdapter implements ActionListener{

        
public void actionPerformed(ActionEvent e){
            
if(popupMenu.isShowing())
                listListener.actionPerformed(e);
            
else
                executeClick();
            }

        
public void mouseClicked(MouseEvent event){
            executeClick();
        }

        
private void executeClick(){
            doSearch();
        }

    }

    
private class ListListener implements ActionListener{
        
public void actionPerformed(ActionEvent e){
                
            keywordField.setText(cacheKeywordList.getSelectedValue().toString());
            popupMenu.setVisible(
false);
            keywordField.requestFocus();
        }

    }

    
private class ResultListModel extends DefaultListModel{
        ResultListModel()
{
            
//load the default cache keyword ---gavin
            for(String matchKeyword : matchResultSet)
                addElement(matchKeyword);
        }

    }


    


}

如果有正好需要这方面功能的可以参考一下,如果您看到的是转载的文章,请记得原地址是http://blog.csdn.net/cleverfoxloving
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22950次
    • 积分:367
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:17条
    文章分类
    最新评论