swing - Implementing auto complete in Java - am I doing it right? -
this question has reply here:
create autocompleting textbox in java dropdown list 3 answers algorithm start input city name - partial or complete if user hits come in , take textjtextfield
begin brute forcefulness search. if matches found, set them in vector
, set in jlist
if no match found, add together string
"no match found" in vector
display jwindow
user containing 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){ } } //------------------------------------------------------------------------------ }
output problem the size of jwindow
displaying results (which jlist
in jscrollpane
) changes based on results - if city name small, jwindow
small, if city name big, jwindow
big.
i have tried every possible combination. tried using setpreferreddimension()
of jwindow
, jlist
, jscrollpane
issue won't go. want match size of decorated jframe
no matter what
jlist
or jcombobox
doesn't returns proper preferredsize
, have set value, utilize jlist.setprototypecellvalue() pack()
jwindow
(must packed after changes) , or jlist.setvisiblerowcount(), value returns getpreferredscrollableviewportsize()
jlist
in jscrollpane
don't utilize keylistener
, utilize documentlistener
(chars can inserted scheme clipboard) jtextcomponents
don't reinvent wheel, utilize autocomplete jcombobox / jtextfield, can redirect / returns result matches popup jwindow
/ undecorated jdialog
(quite best workaround popup recycle)
edit
anyways have manually create list of cities supported right ?? bx @little child
this thought quite easy, can set jtable
jwindow
with 1 column
,
without jtableheader
add there rowsorter (see code illustration in tutorial)
then every steps done :-), nil else required there (maybe bonus alter background
of jtextfield
in case rowfilter
returns no matches, add together setvisible
popup window documentlistener
(be sure test !isvisible
))
java swing autocomplete jframe jwindow
No comments:
Post a Comment