Implement autocomplete in Java – did I do it right?
See English answers > create a autocompleting textbox in java with a dropdown list5
>Start > enter city name – partial or complete > if the user clicks enter, get the text from jtextfield > start search. > If matches are found, put them in vector and JList > if no match is found, add a string "no match found" > show jwindow to the user containing the results > 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){
}
}
//------------------------------------------------------------------------------
}
yield
problem
The size of the jwindow that displays the result (JList in JScrollPane) varies according to the result – if the city name is small, the jwindow is small, if the city name is large, the jwindow is large
I have tried all possible combinations I try to use setpreferreddimension () of jwindow, JList and JScrollPane, but the problem will not disappear I want it to match the size of the decorated JFrame, whatever
Solution
>JList or JCombo@R_644_2419 @If the appropriate preferredSize is not returned, this value must be set to use JList for jwindow Setprototypecellvalue() (must be packaged after any changes) and / or use JList Setvisiblerowcount(), and then return getpreferredscrollableviewportsize() for JList in JScrollPane
edit
>This idea may be easy. You can put JTable to the jwindow > in a column, > no jtableheader > add rowsorter (see the code example in the tutorial) > and then complete each step: -), There is no other need to be there (maybe change the background of jtextfield, and add a setvisible pop-up window from documentlistener (be sure to test! Isvisible) when there is no match returned by rowfilter)
