Implementing auto complete in Java - am I doing it right? [duplicate]
Asked Answered
N

4

11

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

enter image description here

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

Nicias answered 13/2, 2013 at 8:19 Comment(4)
Why don't you add the autocomplete gui below the textfield in the same jframe?Sidestroke
Besides the mentioned problem, your gui has another problem, the autocomplete window does not move with the textfield's frame. This thread[#2428315 solves this problem.Sidestroke
it does move. The setLocation() is relative to the decorated JFrameNicias
If already visible, autocomplete list does not move with the JFrame. It appears at correct location when pressed enter though.Sidestroke
D
7

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))

Dulcia answered 13/2, 2013 at 8:31 Comment(5)
This one is better: #3674307. Anyways so basically I will have to manually create a list of all the cities that are to be supported right ??Nicias
This one is better == the same code, I'll edit my answer hereDulcia
+1 For being a Swing geek :)Oxytocin
@Dulcia I was going over the AutoCompleteTextField by Java 2s and I was wondering if I could download their API as a jar or zip. Is that possible ?Nicias
there are only two classes, (copy ---> paste, nothing else required) one for JTextField, 2nd for JComboBox, you needed both :-)Dulcia
G
1

You need to use the width of the the JFrame every time you initiate the search and use it calculate the width of the list.

Just change the initiateSearch() function like this:

public void initiateSearch(String lookFor){

    //add the following two statements to set the width of the list.
    int newWidth = AutoCompleteTest.this.getSize().width;        
    list.setPreferredSize(new Dimension(newWidth, list.getPreferredSize().height));

        Vector<String> matches = new Vector<String>();
        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();
        }

    }

Here is a sample output:

small size

and

enter image description here

PS: Just for better aesthetics try using some layout to make the text field fill the entire width.

Group answered 13/2, 2013 at 8:32 Comment(0)
O
1

You should use JComboBox, and for the autocompletion, read this article.

Obelisk answered 13/2, 2013 at 8:36 Comment(0)
P
1

One solution would be to change initiateDisplay() to this:

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

        int padding = 5;
        int height = this.searchResult.getModel().getSize()
                * AutoCompleteTest.this.city.getSize().height;
        int windowWidth = AutoCompleteTest.this.getSize().width;

        this.setSize(windowWidth, height);
        this.setVisible(true);
    }
Pulpiteer answered 13/2, 2013 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.