java - Filtering a JList from text field input -
i have text field stick jlist1 , need filter j list entities depend on values entered user in text box below if j list values {"one","two","three"......etc }
if user entered "three" in text box pop list , select row 3 value selected relating index.
private void jtbandnoactionperformed(java.awt.event.actionevent evt) { // todo add together handling code here: string txt=jtbandno.gettext(); jlist2.collections.equal(txt); int index = jlist2.getselectedindex(); joptionpane.showmessagedialog(null, index); }
well can create method filter out values using
defaultlistmodel#removeelement
, and defaultlistmodel#addelement
generally when dealing info or components utilize models, should interact model , not component. can have method like
private string[] defaultvalues = { "a", "ab", "abc", "abcd", "abcde", "abcdef", "absdefg", "abcdefgh", "abcdefghi", "abcdefghij", "abcdefghijk", "abcdefghijkl", "abcdefghijklm", }; public void filtermodel(defaultlistmodel<string> model, string filter) { (string s : defaultvalues) { if (!s.startswith(filter)) { if (model.contains(s)) { model.removeelement(s); } } else { if (!model.contains(s)) { model.addelement(s); } } } }
when need filter, illustration in documentlistener
of text field, text of text field, , phone call method, passing in model , filter string. like
private void filter() { string filter = field.gettext(); filtermodel((defaultlistmodel<string>)jlist.getmodel(), filter); }
here's finish example. may want implement more complicated filtering such including sorting search filter. simple case example.
import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.listmodel; import javax.swing.swingutilities; import javax.swing.event.documentevent; import javax.swing.event.documentlistener; public class dynamiclistdemo { private string[] defaultvalues = { "a", "ab", "abc", "abcd", "abcde", "abcdef", "absdefg", "abcdefgh", "abcdefghi", "abcdefghij", "abcdefghijk", "abcdefghijkl", "abcdefghijklm", }; private jlist jlist = createjlist(); public dynamiclistdemo() { jframe frame = new jframe(); frame.add(new jscrollpane(jlist)); frame.add(createtextfield(), borderlayout.page_end); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } private jtextfield createtextfield() { final jtextfield field = new jtextfield(15); field.getdocument().adddocumentlistener(new documentlistener(){ @override public void insertupdate(documentevent e) { filter(); } @override public void removeupdate(documentevent e) { filter(); } @override public void changedupdate(documentevent e) {} private void filter() { string filter = field.gettext(); filtermodel((defaultlistmodel<string>)jlist.getmodel(), filter); } }); homecoming field; } private jlist createjlist() { jlist list = new jlist(createdefaultlistmodel()); list.setvisiblerowcount(6); homecoming list; } private listmodel<string> createdefaultlistmodel() { defaultlistmodel<string> model = new defaultlistmodel<>(); (string s : defaultvalues) { model.addelement(s); } homecoming model; } public void filtermodel(defaultlistmodel<string> model, string filter) { (string s : defaultvalues) { if (!s.startswith(filter)) { if (model.contains(s)) { model.removeelement(s); } } else { if (!model.contains(s)) { model.addelement(s); } } } } public static void main(string[] args) { swingutilities.invokelater(new runnable(){ public void run() { new dynamiclistdemo(); } }); } }
update: mkorbel pointed out obvious, may improve utilize case jtable
single column, sorting , filtering part of api.
see how utilize tables: sorting , filtering
java swing filtering jlist
No comments:
Post a Comment