在Java中实现自动完成 - 我在做对吗? [英] Implementing auto complete in Java - am I doing it right?

查看:169
本文介绍了在Java中实现自动完成 - 我在做对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

算法


  1. 开始

  2. 输入城市名 - 部分或完全

  3. 如果用户点击进入,采取从的JTextField 文本

  4. 开始蛮力搜索。

  5. 如果找到匹配项,将它们放在一个矢量,并把它放在一个 JList的

  6. 如果没有发现匹配,添加一个字符串无匹配找到在矢量

  7. 显示的JWindow 来包含结果用户

  8. 停止

code:

 封装测试;
进口的javax.swing *。进口java.awt.Dimension中;
java.awt.event中导入*。
进口java.util.Vector中;公共类AutoCompleteTest扩展的JFrame {
    JTextField的城市=新的JTextField(10);
    字符串enteredName = NULL;
    的String [] =城市{新泽西,新罕布什尔
            苏塞克斯,埃塞克斯,伦敦,德里,纽约};
    JList的列表=新的JList();
    JScrollPane的窗格=新JScrollPane的();
    ResultWindow R =新ResultWindow();
// ------------------------------------------------ ------------------------------
    公共静态无效的主要(字串[] args){
        新AutoCompleteTest();
    }
// ------------------------------------------------ ------------------------------
    公共AutoCompleteTest(){
        的setLayout(新java.awt.FlowLayout中());
        调用setVisible(真);
        加(市);
//添加(面板);
        包();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        city​​.addKeyListener(新TextHandler());
    }
// ------------------------------------------------ ------------------------------
    公共无效initiateSearch(字符串lookFor){
        矢量<串GT;比赛=新的矢量<>();
        lookFor = lookFor.toLowerCase();
        对于(每个字符串:市){
            如果(each.contains(lookFor)){
                matches.add(个);
                的System.out.println(匹配+每);
            }
        }
        this.repaint();        如果(matches.size()!= 0){
            list.setListData(匹配);
            r.searchResult =清单;
            r.pane =窗格;
            r.initiateDisplay();
        }其他{
            matches.add(无匹配找到);
            list.setListData(匹配);
            r.searchResult =清单;
            r.pane =窗格;
            r.initiateDisplay();
        }    }
// ------------------------------------------------ ------------------------------
    公共类ResultWindow扩展的JWindow {
        公共JScrollPane的窗格;
        公共JList的信息搜索结果;
// ------------------------------------------------ ------------------------------
        公共ResultWindow(){        }
// ------------------------------------------------ ------------------------------
        公共无效initiateDisplay(){
            pane.setViewportView(信息搜索结果);
            加(面板);
            包();
            this.setLocation(AutoCompleteTest.this.getX()+ 2,
                    AutoCompleteTest.this.getY()+
                    AutoCompleteTest.this.getHeight());// this.set preferredSize(city.get preferredSize());
            this.setVisible(真);
        }
    }
// ------------------------------------------------ ------------------------------    类TextHandler实现的KeyListener {
        @覆盖
        公共无效的keyTyped(KeyEvent的E){        }        @覆盖
        公共无效键pressed(KeyEvent的E){
            如果(r.isVisible()){
                r.setVisible(假);
            }
            如果(e.getKeyChar()=='\\ n'){
                initiateSearch(city.getText());
            }
        }        @覆盖
        公共无效调用keyReleased(KeyEvent的E){        }
    }
// ------------------------------------------------ ------------------------------
}

输出

问题

大小的的JWindow 显示结果(这是一个 JList的根据结果​​JScrollPane的)的变化 - 如果这个城市的名字是小,的JWindow 是小,如果城市名大,的JWindow 大。

我已经试过所有可能的组合。我试着用集preferredDimension()的JWindow JList的 JScrollPane的,但这个问题不会去。结果
我希望它匹配的大小装饰的JFrame 不管是什么


解决方案

修改


  

不管怎么说所以基本上我将不得不手动创建的所有列表
  城市要被支持的权利? BX @Little儿童



  • 这种想法可能是相当容易的,你可以把 的JTable 的JWindow


  • 一个


  • 没有 JTableHeader的


  • 添加有 RowSorter的(见$ C教程中的$ C为例)


  • 然后每步骤​​完成:-),没有别的要求没有(也许奖金来改变背景 的JTextField 的RowFilter 没有返回比赛,从调用setVisible 的弹出窗口的情况下>的DocumentListener (一定要测试!ISVISIBLE ))


Algorithm

  1. Start
  2. Input a city name - partial or complete
  3. If the user hits enter , take the text from JTextField
  4. Begin brute force search.
  5. If the matches are found, put them in a Vector and put it in a JList
  6. If no match is found, add a String "No Match Found" in Vector
  7. Display JWindow to user containing the results
  8. Stop

Code:

package test;
import javax.swing.*;

import java.awt.Dimension;
import java.awt.event.*;
import java.util.Vector;

public class AutoCompleteTest extends JFrame{
    JTextField city = new JTextField(10);
    String enteredName = null;
    String[] cities = {"new jersey","new hampshire",
            "sussex","essex","london","delhi","new york"};
    JList list = new JList();
    JScrollPane pane = new JScrollPane();
    ResultWindow r = new ResultWindow();
//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new AutoCompleteTest();
    }
//------------------------------------------------------------------------------
    public AutoCompleteTest(){
        setLayout(new java.awt.FlowLayout());
        setVisible(true);
        add(city);
//      add(pane);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        city.addKeyListener(new TextHandler());
    }
//------------------------------------------------------------------------------
    public void initiateSearch(String lookFor){
        Vector<String> matches = new Vector<>();
        lookFor = lookFor.toLowerCase();
        for(String each : cities){
            if(each.contains(lookFor)){
                matches.add(each);
                System.out.println("Match: " + each);
            }
        }
        this.repaint();

        if(matches.size()!=0){
            list.setListData(matches);
            r.searchResult = list;
            r.pane = pane;
            r.initiateDisplay();
        }else{
            matches.add("No Match Found");
            list.setListData(matches);
            r.searchResult = list;
            r.pane = pane;
            r.initiateDisplay();
        }

    }
//------------------------------------------------------------------------------
    public class ResultWindow extends JWindow{
        public JScrollPane pane;
        public JList searchResult;
//------------------------------------------------------------------------------
        public ResultWindow(){

        }
//------------------------------------------------------------------------------
        public void initiateDisplay(){
            pane.setViewportView(searchResult);
            add(pane);
            pack();
            this.setLocation(AutoCompleteTest.this.getX() + 2, 
                    AutoCompleteTest.this.getY()+
                    AutoCompleteTest.this.getHeight());

//          this.setPreferredSize(city.getPreferredSize());
            this.setVisible(true);
        }
    }
//------------------------------------------------------------------------------

    class TextHandler implements KeyListener{
        @Override
        public void keyTyped(KeyEvent e){

        }

        @Override
        public void keyPressed(KeyEvent e){
            if(r.isVisible()){
                r.setVisible(false);
            }
            if(e.getKeyChar() == '\n'){
                initiateSearch(city.getText());
            }
        }

        @Override
        public void keyReleased(KeyEvent e){

        }
    }
//------------------------------------------------------------------------------
}

Output

Problem

The size of the JWindow displaying the results (which is a JList in a JScrollPane) changes based on the results - if the city name is small, JWindow is small, if the city name is big, JWindow is big.

I have tried every possible combination. I tried using setPreferredDimension() of the JWindow, the JList and JScrollPane but the issue won't go.
I want it to match the size of the decorated JFrame no matter what

解决方案

EDIT

Anyways so basically I will have to manually create a list of all the cities that are to be supported right ?? bx @Little Child

  • this idea could be quite easy, you can to put JTable to the JWindow

  • with one Column,

  • without JTableHeader

  • add there RowSorter (see code example in tutorial)

  • then every steps are done :-), nothing else is required there (maybe bonus to change Background of JTextField in the case that RowFilter returns no matches, add setVisible for popup window from DocumentListener (be sure to test for !isVisible))

这篇关于在Java中实现自动完成 - 我在做对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆