java - JCheckBox selecting 1st option automatically -


i'm developing system that, once selected field of jcombobox, change text of jbutton, connected offline depending on status of field written in jcombobox.

all has been done, have problem.

when run program, system automatically enters event addactionlistener, not have ever selected field of jcombobox?

can give me hand?

edited in answer

... block of code (under) wrote method executed before combobox.addactionlistener. code, wrote, beacause have need popolate dinamically elements of jcombobox...

private void actionofsearch(){  table.addmouselistener(new mouseadapter(){      public void mouseclicked(mouseevent me){      if(ipavailable){         blocksetremoteunit = false;         timer = new timer(10000, new actionlistener() {             public void actionperformed(actionevent e) {             //extracted_values vector<stirng>             if(typeofconnection.equals("abc") || typeofconnection.equals("def"))                 extracted_values = new e_connectionabc_def(resourceselected, credential).result;             else{                 try {                     extracted_values = new e_connectionother(resourceselected, credential).result;                 } catch (interruptedexception e1) { e1.printstacktrace(); }             }             int size = extracted_values.elementat(0).size();             string stringofnamelabel = "";             string stringvalueofresource = "";             jlabel nameresource = null;             jprogressbar progressbar = null;             subpanelofinfoserver.removeall();             subpanelofinfoserver.repaint();             subpanelofinfoserver.revalidate();             if(!blocksetremoteunit)                 combobox.additem(" ");             for(int i=0;i<size;i++){                 stringofnamelabel = extracted_values.elementat(1).elementat(i);                 //here popolate fields of jcombobox, , how can see,                 //i can change fields, every time selected different row                 //of jtable.                         if(!blocksetremoteunit && stringofnamelabel.length()==2 && stringofnamelabel.contains(":"))                     combobox.additem(stringofnamelabel);                          nameresource = new jlabel(stringofnamelabel);                 nameresource.sethorizontalalignment(swingconstants.center);                 stringvalueofresource = extracted_values.elementat(0).elementat(i);                 progressbar = new jprogressbar();                 progressbar.setvalue(integer.parseint(stringvalueofresource));                  subpanelofinfoserver.add(nameresource);                 subpanelofinfoserver.add(progressbar);             }             blocksetremoteunit = true;             }        });         timer.setrepeats(true);         timer.setcoalesce(true);         timer.setinitialdelay(0);         timer.start();     }     else{         logger.warn("[jtable-selectrow]attention! " + resourceselected + " not available");         joptionpane.showmessagedialog(null, "ip " + resourceselected + " not available");     }        }}); } 

now, in other java method, (that it's call after), find code:

private jpanel connectresource(){     jpanel remoteresource = new jpanel();     jpanel panelsouth = new jpanel();     //jstmpd vector<vector<string>>     jstmpd  = getalreadymapped();     remoteresource.setlayout(new borderlayout());      final jbutton resourceconnect = new jbutton();      combobox.addactionlistener (new actionlistener () {         public void actionperformed(actionevent e) {             object letterereadcombobox = combobox.getselecteditem();             final string letter = letterereadcombobox.tostring();              try {                 alreadymapped = ctrifalreadymapped(resourceselected, letter, typeofconnection, jstmpd);             } catch (ioexception e1) {             } catch (interruptedexception e1) {             }              if(alreadymapped)                 resourceconnect.seticon(new imageicon(path + "src/img/disconnect.png"));             else                 resourceconnect.seticon(new imageicon(path + "src/img/drive-network-connected.png"));              resourceconnect.addactionlistener( new actionlistener(){                 public void actionperformed(actionevent ae) {                     try {                         alreadymapped = ctrifalreadymapped(resourceselected, letter, typeofconnection, jstmpd);                     } catch (ioexception | interruptedexception e1) {                     }                     if(alreadymapped){                         try {                             string locallettermounted = deleteelement(resourceselected, letter, jstmpd);                             if(locallettermounted!=""){                                 new e_mapped_drives().unmountresource(locallettermounted);                                 resourceconnect.seticon(new imageicon(path + "src/img/drive-network-connected.png"));                                 resourceconnect.setactioncommand("!alreadymapped");                             }                         } catch (ioexception e) {                              joptionpane.showmessagedialog(null, "you didn't selected letter of remote resource!\n please re-try");                         }                     }                     else{                         string lettergetted = "";                         string lettermounted= "";                         if(typeofconnection.equals("rdp") || typeofconnection.equals("vnc")){                             try {                                 lettergetted = letter;                                 lettermounted = new e_mapped_drives().mountresourcewindows(resourceselected, lettergetted, credential);                                 jstmpd.addelement(new vector<string>(arrays.aslist(lettergetted, resourceselected,typeofconnection,lettermounted)));                                 resourceconnect.seticon(new imageicon(path + "src/img/disconnect.png"));                             } catch (ioexception e) {                                 logger.warn("[bottone-resourceconnect][ioexception] didn't selected letter!");                                 joptionpane.showmessagedialog(null, "you didn't selected letter of remote resource!\n please re-try");                             }                         }                         else{                             try {                                 lettergetted = new e_mapped_drives().mountresourcelinux(resourceselected, credential);//, combobox.getselecteditem(), credential);                                 resourceconnect.seticon(new imageicon(path + "src/img/disconnect.png"));                             } catch (ioexception e) {                                 joptionpane.showmessagedialog(null, "you didn't selected letter of remote resource!\n please re-try");                             }                         }                     }                 }             });         }     });      panelsouth.setbackground(getcolorbackground());     remoteresource.add(combobox, borderlayout.north);     remoteresource.add(resourceconnect, borderlayout.center);     remoteresource.add(panelsouth, borderlayout.south);     return remoteresource; } 

probably creating action listener before adding items jcombobox, or programmatically selecting value of combo box after registering action istener. create action listener after items added jcombobox.

this happens because if combobox has no items, when add item becomes selected value, firing action listener.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -