java - First item getting selected in listbox -


i want select items keywords facebook , twitter in it. problem first item "aggregator" gets selected. can tell me wrong in code. thanks.

   int j=0;    jlist1.setmodel(listmodel);    (int i=0;i<listmodel.size();i++){         if (listmodel.getelementat(i).tostring().indexof("facebook")!=-1||listmodel.getelementat(i).tostring().indexof("twitter")!=-1){            a[j]=i;             j++;        }    }      jlist1.setselectedindices(a); 

to select multiple elements, can use jlist#addselectioninterval, example...

list

import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jscrollpane; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class listselectionexample {      public static void main(string[] args) {         new listselectionexample();     }      public listselectionexample() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  defaultlistmodel model = new defaultlistmodel();                 model.addelement("bananas on facebook");                 model.addelement("bananas in pajamas");                 model.addelement("teddies in space");                 model.addelement("teddies on twitter");                 model.addelement("rat on moon");                 model.addelement("cow on moon");                 model.addelement("facebook pandas");                  jlist list = new jlist(model);                 (int index = 0; index < model.getsize(); index++) {                     string text = model.getelementat(index).tostring().tolowercase();                     if (text.contains("facebook") || text.contains("twitter")) {                         list.addselectioninterval(index, index);                     }                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new jscrollpane(list));                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }  } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -