java - How to refresh the JComboBox data? -


i having trouble refreshing data inside jcombobox.

there button "create" has actionlistener, adds item jcombobox.

but changes not reflected in gui: still don't see new added item.

repaint() doesn't help.

update: here (almost) full gui code:

public class main extends jframe implements actionlistener {     static connection conn;     static preparedstatement ps = null;     static resultset res;      static statement sta;  private final static int iteration_number = 1000;  public void gui () throws sqlexception {     setbounds(0, 0, 320, 240);     addwindowlistener(new windowadapter(){     public void windowclosing(windowevent we){         close(ps);         close(res);         close(conn);         system.exit(0);         }     });     setminimumsize(new dimension(320, 240));     setresizable(false);      this.settitle("accounts");      jpanel panel = new jpanel();     gridlayout2 gl = new gridlayout2(4,3);     gl.sethgap(10);     panel.setlayout(gl);      font font = new font("serif", font.bold, 20);     font font2 = new font("courier new", font.bold, 16);      jlabel label1 = new jlabel("username");     jlabel label2 = new jlabel("password");     jlabel label3 = new jlabel("controls");      label1.setfont(font2);     label2.setfont(font2);     label3.setfont(font2);      final jtextfield username = new jtextfield();     final jtextfield password1 = new jpasswordfield();     final jtextfield password2 = new jpasswordfield();      final jcombobox userbox1 = new jcombobox();     final jcombobox userbox2 = new jcombobox();      jbutton create = new jbutton("create");      create.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e)         {             try {                 createuser(conn, username.gettext(), password1.gettext()); userbox1.additem(username.gettext());                 userbox2.additem(username.gettext());             } catch (nosuchalgorithmexception                     | unsupportedencodingexception | sqlexception e1) {                 // todo auto-generated catch block                 e1.printstacktrace();             }         }     });        userbox1.removeallitems();     userbox2.removeallitems();      res = (resultset) sta.executequery("select login accounts");      string temp;      (int i=0; res.next(); i++) {         temp = (string)res.getstring("login");         userbox1.additem(temp);         userbox2.additem(temp);     }      panel.add(label1);     panel.add(label2);     panel.add(label3);      panel.add(username);     panel.add(password1);     panel.add(create);      panel.add(userbox1);     panel.add(password2);     panel.add(modify);      panel.add(userbox2);     panel.add(new jlabel(""));     panel.add(delete);      add(panel);      setvisible(true); } 

solution: adding password1.settext(""); after "createuser" solved problem! that's strange, maybe somehow refreshed gui...

  • you have add comboboxmodel jcombobox,

  • there can add / remove / modify value,

  • events implemented in api refreshing view (jcombobox) without code lines

  • all updates must done on event dispatch thread

edit

maybe missread question, if want add jcombobox visible gui, have call (as last code lines , success once 1 container)

mycontainer.revalidate() // jframe java7 there validate() mycontainer.repaint() 

(sorry @timaschew)


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -