java - JComboBox Option Selected Not Imposing Change -


in code have simple jcombobox used turn feature on or off. jcombobox code is:

public static jcombobox geterrorloggingonoroff(){     string[] options = { "on", "off" };     jcombobox combo = new jcombobox(options);     return combo; } 

the issue returns value being on regardless of when click "off." jcombobox called here: note have included system.out.println()'s (and check null) check output.

string option = combo.getselecteditem().tostring(); .... if(option.equals("on")){     system.out.println("on selected");     return; } else if(option.equals("off")){     system.out.println("off selected");     return; } else {     system.out.println("null value option.... :-/"); } 

any pointers appreciated, genuinely baffled why occurring, no doubt simple on sight.

regards, reg.

edit here full exerpt of code

 final jmenuitem enableerrorlogging = new jmenuitem("enable error logging");     enableerrorlogging.setaccelerator(keystroke.getkeystroke(keyevent.vk_m, actionevent.alt_mask));     enableerrorlogging.setmnemonic(keyevent.vk_m);     menu.add(enableerrorlogging);     enableerrorlogging.addactionlistener( new actionlistener() {          public void actionperformed(final actionevent e) {             if (ui.getworkspace().getcurrentproject() == null) {                 openprojectmessagepane();                 return;             }              jcombobox combo = geterrorloggingonoroff();             jpanel panel = new jpanel(new gridlayout(0, 1));             panel.add(new jlabel("turn error logging on or off"));             panel.add(combo);             string option = combo.getselecteditem().tostring();              try{              int r = joptionpane.showconfirmdialog(frame, panel, "enable error logging?",                     joptionpane.ok_option, joptionpane.plain_message);             if (r == joptionpane.ok_option) {             if(option.equals("on")){                                  system.out.println("on selected");                 return;             }             else if(option.equals("off")){                 system.out.println("off selected");                 return;             }             else{                 system.out.println("null value option.... :-/");             }                 }             } catch (exception ex) {                 logger.debug(ex);             }         }     }); 

use .equals comparing strings, not ==

i.e.

if(option.equals("on")) {     // ... } else if(option.equals("off")) {     // ... } 

here might problem:

jcombobox combo = geterrorloggingonoroff(); jpanel panel = new jpanel(new gridlayout(0, 1)); panel.add(new jlabel("turn error logging on or off")); panel.add(combo);  // here, seem getting 'selected item' before // showing using joptionpane, believe null default string option = combo.getselecteditem().tostring();  try {     int r = joptionpane.showconfirmdialog(frame, panel, "enable error logging?",         joptionpane.ok_option, joptionpane.plain_message);     if (r == joptionpane.ok_option) {             if(option.equals("on")) {                 // ...             }     } } 

place string option = combo.getselecteditem().tostring(); after call joptionpane.showconfirmdialog(...).


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -