java - I am using a JOptionPane to show error message but when I click ok on the error message it does not close the message -


i making unit converter , have ran issue joptionpane.showmessagedialog code complete , should run fine.

the converter shows message dialog if put both converting units same unit. when message window pops , says message ok button click. when click ok button not exit message dialog. copied line of code different longer version of converter , works fine it. if have suggestions how math better please tell me. still beginner programming , java. appreciated.

 package v3converter;   import java.awt.toolkit; import java.math.biginteger; import java.util.hashmap; import javax.swing.defaultcomboboxmodel; import javax.swing.joptionpane;  /** * * @author anthony tennenbaum */ public class v3converter extends javax.swing.jframe {       //constant type double arrays filled in order of: centimeter,feet,inche,kilometer,mile,millimeter,meter,yard     final double[] to_centm = {1,30.48,2.54,100000,160935,0.1,100,91.44};     final double[] to_feet = {0.032808399,1,0.083333333334,3280.839895,5280.019685,0.0032808399,3.280839895,3};     final double[] to_inche ={0.3937007874,12,1,39370.07874,63360.23622, 0.0393700787,39.37007874,36} ;     final double[] to_kilom = { 0.00001,0.0003048, } ;     final double[] to_mile = {};     final double[] to_milim = {};     final double[] to_meter = {0.01,0.3048,0.0254,0.001,1609.35,0.001,1,0.9144};      final double[] to_yard = {0.010936133,0.3333333333,0.0277777778,1093.6132983,1760.0065617,1760.0065617,1};      //making string array     final string[] unit_name = {"centimeters", "feet", "inches", "kilometers", "miles", "millimeters", "meters", "yards"};a    /**  * creates new form converter  */ public v3converter() {     initcomponents();     seticon();  //sets icon image jframe     setsize(350,225);  //set starting size of frame     setlocationrelativeto(null);     //on startup centers frame     getcontentpane().setbackground(java.awt.color.black);     comboboxfrom.setselectedindex(0);    //combobox starts @ index 0(first item in list) -1 puts box empty     comboboxto.setselectedindex(0);  }   @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">                           private void initcomponents() {      btnconvert = new javax.swing.jbutton();     comboboxfrom = new javax.swing.jcombobox<>();     comboboxto = new javax.swing.jcombobox<>();     txtfield1 = new javax.swing.jtextfield();     btnreset = new javax.swing.jbutton();     btnexit = new javax.swing.jbutton();     lblconvertto = new javax.swing.jlabel();     lblconvertfrom = new javax.swing.jlabel();     pnlanswer = new javax.swing.jpanel();     lblanswer = new javax.swing.jlabel();      setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);     settitle("unit converter");     setfont(new java.awt.font("copperplate gothic light", 1, 14)); // noi18n     setresizable(false);      btnconvert.setbackground(new java.awt.color(204, 0, 0));     btnconvert.setfont(new java.awt.font("tahoma", 1, 12)); // noi18n     btnconvert.setforeground(new java.awt.color(0, 204, 0));     btnconvert.settext("convert");     btnconvert.setactioncommand("convert to");     btnconvert.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             btnconvertactionperformed(evt);         }     });      comboboxfrom.setmodel(new javax.swing.defaultcomboboxmodel<>(new string[] { "choose one..", "length units", "time units" }));     comboboxfrom.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             comboboxfromactionperformed(evt);         }     });      comboboxto.setmodel(new javax.swing.defaultcomboboxmodel<>(new string[] { "choose one..", " " }));     comboboxto.settooltiptext("");     comboboxto.setenabled(false);     comboboxto.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             comboboxtoactionperformed(evt);         }     });      txtfield1.setfont(new java.awt.font("tahoma", 1, 13)); // noi18n     txtfield1.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             txtfield1actionperformed(evt);         }     });      btnreset.setbackground(new java.awt.color(0, 204, 0));     btnreset.setfont(new java.awt.font("tahoma", 1, 12)); // noi18n     btnreset.setforeground(new java.awt.color(204, 0, 0));     btnreset.settext("reset");     btnreset.setactioncommand("convert to");     btnreset.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             btnresetactionperformed(evt);         }     });      btnexit.setbackground(new java.awt.color(0, 0, 204));     btnexit.setfont(new java.awt.font("tahoma", 1, 12)); // noi18n     btnexit.setforeground(new java.awt.color(204, 0, 0));     btnexit.settext("exit");     btnexit.setactioncommand("convert to");     btnexit.addactionlistener(new java.awt.event.actionlistener() {         public void actionperformed(java.awt.event.actionevent evt) {             btnexitactionperformed(evt);         }     });      lblconvertto.setfont(new java.awt.font("copperplate gothic light", 1, 11)); // noi18n     lblconvertto.sethorizontalalignment(javax.swing.swingconstants.center);     lblconvertto.settext("convert to");      lblconvertfrom.setfont(new java.awt.font("copperplate gothic light", 1, 11)); // noi18n     lblconvertfrom.sethorizontalalignment(javax.swing.swingconstants.center);     lblconvertfrom.settext("convert from");      pnlanswer.setborder(javax.swing.borderfactory.createlineborder(new java.awt.color(0, 0, 0), 2));      lblanswer.setfont(new java.awt.font("tahoma", 1, 14)); // noi18n     lblanswer.sethorizontalalignment(javax.swing.swingconstants.center);      javax.swing.grouplayout pnlanswerlayout = new javax.swing.grouplayout(pnlanswer);     pnlanswer.setlayout(pnlanswerlayout);     pnlanswerlayout.sethorizontalgroup(         pnlanswerlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(pnlanswerlayout.createsequentialgroup()             .addcomponent(lblanswer, javax.swing.grouplayout.preferred_size, 154, javax.swing.grouplayout.preferred_size)             .addgap(0, 0, short.max_value))     );     pnlanswerlayout.setverticalgroup(         pnlanswerlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(javax.swing.grouplayout.alignment.trailing, pnlanswerlayout.createsequentialgroup()             .addgap(0, 0, short.max_value)             .addcomponent(lblanswer, javax.swing.grouplayout.preferred_size, 35, javax.swing.grouplayout.preferred_size))     );      javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane());     getcontentpane().setlayout(layout);     layout.sethorizontalgroup(         layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(layout.createsequentialgroup()             .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                 .addgroup(layout.createsequentialgroup()                     .addgap(266, 266, 266)                     .addcomponent(btnexit))                 .addgroup(layout.createsequentialgroup()                     .addgap(22, 22, 22)                     .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                         .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing)                             .addgroup(layout.createsequentialgroup()                                 .addcomponent(btnconvert)                                 .addgap(45, 45, 45)                                 .addcomponent(btnreset)                                 .addgap(196, 196, 196))                             .addgroup(javax.swing.grouplayout.alignment.leading, layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing)                                 .addgroup(layout.createsequentialgroup()                                     .addcomponent(pnlanswer, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                                     .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                                     .addcomponent(comboboxto, javax.swing.grouplayout.preferred_size, 135, javax.swing.grouplayout.preferred_size))                                 .addcomponent(lblconvertto, javax.swing.grouplayout.preferred_size, 295, javax.swing.grouplayout.preferred_size)))                         .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false)                             .addcomponent(lblconvertfrom, javax.swing.grouplayout.alignment.leading, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)                             .addgroup(layout.createsequentialgroup()                                 .addcomponent(txtfield1, javax.swing.grouplayout.preferred_size, 157, javax.swing.grouplayout.preferred_size)                                 .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                                 .addcomponent(comboboxfrom, javax.swing.grouplayout.preferred_size, 135, javax.swing.grouplayout.preferred_size))))))             .addcontainergap(421, short.max_value))     );     layout.setverticalgroup(         layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(layout.createsequentialgroup()             .addcontainergap()             .addcomponent(lblconvertfrom)             .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)             .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                 .addcomponent(comboboxfrom, javax.swing.grouplayout.preferred_size, 39, javax.swing.grouplayout.preferred_size)                 .addcomponent(txtfield1, javax.swing.grouplayout.preferred_size, 39, javax.swing.grouplayout.preferred_size))             .addgap(18, 18, 18)             .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline)                 .addcomponent(btnconvert)                 .addcomponent(btnreset)                 .addcomponent(btnexit))             .addgap(18, 18, 18)             .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                 .addgroup(layout.createsequentialgroup()                     .addcomponent(pnlanswer, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                     .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                     .addcomponent(lblconvertto))                 .addcomponent(comboboxto, javax.swing.grouplayout.preferred_size, 39, javax.swing.grouplayout.preferred_size))             .addcontainergap(324, short.max_value))     );      pack();     setlocationrelativeto(null); }// </editor-fold>                          private void comboboxfromactionperformed(java.awt.event.actionevent evt) {                                                   //string lengthunits[] = {"centimeters", "feet", "inches", "kilometers", "light years", "miles", "millimeters", "meters", "yards" };  //makes array called lengthunits populates     string timeunits[] = {"hours", "milliseconds", "minutes", "seconds"};     if(comboboxfrom.getselecteditem().equals("length units")){   //if true          comboboxto.setenabled(true);  //set comboboxto activated         //defaultcomboboxmodel time = new defaultcomboboxmodel(timeunits);          comboboxfrom.setmodel( new defaultcomboboxmodel( unit_name ) );  //sets model of comboboxfrom array lengthunits         comboboxto.setmodel( new defaultcomboboxmodel( unit_name) );    //sets model of comboboxto array lengthunits     }     else if(comboboxfrom.getselecteditem().equals("time units")){         comboboxto.setenabled(true);   //set comboboxto activated         //defaultcomboboxmodel time = new defaultcomboboxmodel(timeunits);  //makes new model , fills array timeunits , link 2 combobox's         comboboxfrom.setmodel( new defaultcomboboxmodel( timeunits ) );  //also makes new model , fills array timeunits         comboboxto.setmodel( new defaultcomboboxmodel( timeunits ) );   //but doesn't link oombobox's      }  }                                              private void comboboxtoactionperformed(java.awt.event.actionevent evt) {                                             }                                            private void btnresetactionperformed(java.awt.event.actionevent evt) {                                              string combostart[] = {"choose one..", "length units", "time units"};     lblanswer.settext(null);   //making lable clear     txtfield1.settext(null);       //making textfield clear     defaultcomboboxmodel reset = new defaultcomboboxmodel(combostart);     comboboxfrom.setmodel(reset);  //set model of comboboxfrom default setting     comboboxto.setmodel(reset);     comboboxfrom.setselectedindex(0); // starts combobox @ index 0. type -1 start empty     comboboxto.setselectedindex(0);     comboboxto.setenabled(false);   //deactivates combboxto     lblanswer.settext(null);   //sets lable or answer displays, empty   }                                          private void btnexitactionperformed(java.awt.event.actionevent evt) {                                              system.exit(0);    //closes program }                                         private void btnconvertactionperformed(java.awt.event.actionevent evt) {                                                 string userinput = txtfield1.gettext(); //userinput equals whatever user typed in txtfield1      try{         double.parsedouble(userinput); //checks see if userinput txtfield1 integer if not catch takes control          for(string s: unit_name){             if(comboboxfrom.getselecteditem().equals(comboboxto.getselecteditem())){                 joptionpane.showmessagedialog(null, "cannot convert same unit type.", "um, forgot something!!", joptionpane.information_message);             }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("centimeters")){                 double answer = (double.parsedouble(userinput) * to_centm[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));             }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("feet")){                 double answer = (double.parsedouble(userinput) * to_feet[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("inches")){                 double answer = (double.parsedouble(userinput) * to_inche[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("kilometers")){                 double answer = (double.parsedouble(userinput) * to_kilom[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("miles")){                 double answer = (double.parsedouble(userinput) * to_mile[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("millimeters")){                 double answer = (double.parsedouble(userinput) * to_milim[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("meters")){                 double answer = (double.parsedouble(userinput) * to_meter[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }             else if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("yards")){                 double answer = (double.parsedouble(userinput) * to_yard[comboboxfrom.getselectedindex()]) ;   //converts userinput type double makes unit conversion                 lblanswer.settext(string.valueof(answer));              }         }                         }     catch(numberformatexception e){        joptionpane.showmessagedialog(this, "you must type number", "hey idiot!!", joptionpane.information_message);      } }                                            private void txtfield1actionperformed(java.awt.event.actionevent evt) {                                               // todo add handling code here: }                                           /**  * @param args command line arguments  */ public static void main(string[] args) {     /* set nimbus , feel */     //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) ">     /* if nimbus (introduced in java se 6) not available, stay default , feel.      * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html       */     try {         (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) {             if ("nimbus".equals(info.getname())) {                 javax.swing.uimanager.setlookandfeel(info.getclassname());                 break;             }         }     } catch (classnotfoundexception ex) {         java.util.logging.logger.getlogger(v3converter.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (instantiationexception ex) {         java.util.logging.logger.getlogger(v3converter.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (illegalaccessexception ex) {         java.util.logging.logger.getlogger(v3converter.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (javax.swing.unsupportedlookandfeelexception ex) {         java.util.logging.logger.getlogger(v3converter.class.getname()).log(java.util.logging.level.severe, null, ex);     }     //</editor-fold>     //</editor-fold>     //</editor-fold>     //</editor-fold>      /* create , display form */     java.awt.eventqueue.invokelater(new runnable() {         public void run() {             new v3converter().setvisible(true);         }     }); }  // variables declaration - not modify                      private javax.swing.jbutton btnconvert; private javax.swing.jbutton btnexit; private javax.swing.jbutton btnreset; private javax.swing.jcombobox<string> comboboxfrom; private javax.swing.jcombobox<string> comboboxto; private javax.swing.jlabel lblanswer; private javax.swing.jlabel lblconvertfrom; private javax.swing.jlabel lblconvertto; private javax.swing.jpanel pnlanswer; static javax.swing.jtextfield txtfield1; // end of variables declaration                        } 

your code appears calling joptionpane multiple times within loop, , you'll want move:

if(comboboxfrom.getselecteditem().equals(comboboxto.getselecteditem())){ 

before loop. don't want call multiple times. , call return; after out of listener. in fact, move before try block:

private void btnconvertactionperformed(java.awt.event.actionevent evt) {                                                string userinput = txtfield1.gettext(); //userinput equals whatever user typed in txtfield1      // check out *** here ***     if(comboboxfrom.getselecteditem().equals(comboboxto.getselecteditem())){         joptionpane.showmessagedialog(null, "cannot convert same unit type.",                  "um, forgot something!!", joptionpane.information_message);         return;     }      try{         double.parsedouble(userinput); //checks see if userinput txtfield1 integer if not catch takes control          for(string s: unit_name){             if((comboboxfrom.getselecteditem().equals(s)) && comboboxto.getselecteditem().equals("centimeters")){ 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -