{"id":1634,"date":"2016-04-25T23:43:00","date_gmt":"2016-04-26T04:43:00","guid":{"rendered":"http:\/\/linuxitos.com\/main\/validar-jtextfield-que-acepte-numeros-decimales\/"},"modified":"2016-04-25T23:43:00","modified_gmt":"2016-04-26T04:43:00","slug":"validar-jtextfield-que-acepte-numeros-decimales","status":"publish","type":"post","link":"https:\/\/linuxitos.com\/main\/validar-jtextfield-que-acepte-numeros-decimales\/","title":{"rendered":"Validar JTextField que acepte n\u00fameros decimales"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-clxqoMfiwIE\/Vx7uFoq9uVI\/AAAAAAAAB3A\/CQXKIg2zsx4o5BAz20RUfhv-0mdPxIHtwCLcB\/s1600\/Captura%2Bde%2Bpantalla%2Bde%2B2016-04-25%2B23-26-03.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"68\" src=\"https:\/\/2.bp.blogspot.com\/-clxqoMfiwIE\/Vx7uFoq9uVI\/AAAAAAAAB3A\/CQXKIg2zsx4o5BAz20RUfhv-0mdPxIHtwCLcB\/s640\/Captura%2Bde%2Bpantalla%2Bde%2B2016-04-25%2B23-26-03.png\" width=\"640\" \/><\/a><\/div>\n<p><\/p>\n<div style=\"text-align: justify;\">A veces es necesario validar campos de texto de tal modo que el usuario cometa el m\u00ednimo error posible, ya que siempre es posible que lo haga.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">Recientemente necesit\u00e9 validar unos jtextfield, en donde soloa aceptaban n\u00fameros decimales, pero no reduje el c\u00f3digo siguiente:<\/div>\n<div style=\"text-align: justify;\">Si alguien conoce alg\u00fan m\u00e9tod mucho menor, favor de comentar:<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"box-sizing: border-box; color: #444444; font-family: Roboto, Verdana, Helvetica, sans-serif; font-size: 15px; line-height: 22.5px; margin-bottom: 25px; margin-top: 25px; text-align: center;\"><\/div>\n<div style=\"box-sizing: border-box; color: #444444; font-family: Roboto, Verdana, Helvetica, sans-serif; font-size: 15px; line-height: 22.5px; margin-bottom: 25px; margin-top: 25px;\"><\/div>\n<div style=\"background: url(http:\/\/blog.desdelinux.net\/wp-content\/themes\/DesdeLinuxV5_new\/img\/alert.png) 7px 50% no-repeat rgb(242, 222, 222); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgb(238, 211, 215); box-sizing: border-box; color: #902323; font-family: Roboto, Verdana, Helvetica, sans-serif; font-size: 14px; line-height: 22.5px; margin: 15px; padding: 10px 10px 10px 45px;\"><span style=\"color: #20124d;\"><span style=\"font-family: Verdana,sans-serif;\">if(jtxtPeso.getText().length() &gt;= 8){<\/span><br \/><span style=\"font-family: Verdana,sans-serif;\"><\/span><span style=\"font-family: Verdana,sans-serif;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; evt.consume();<\/span><br \/><span style=\"font-family: Verdana,sans-serif;\"><\/span><\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"color: #20124d;\"><span style=\"font-family: Verdana,sans-serif;\">}<br \/>&nbsp;char car = evt.getKeyChar();<br \/>&nbsp;char text[];<br \/>&nbsp;int count = 0; <br \/>&nbsp;text = jtxtPeso.getText().toCharArray();<br \/>&nbsp;for(int i = 0 ; i&lt; text.length; i++){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(text[i] == &#8216;.&#8217;){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count++;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;}<br \/>&nbsp;if(count&gt;=1 &amp;&amp; evt.getKeyChar() == &#8216;.&#8217;){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; evt.consume();<br \/>&nbsp;}<br \/>&nbsp; if((car!=&#8217;0&#8242; &amp;&amp; car!=&#8217;1&#8242; &amp;&amp; car!=&#8217;2&#8242; &amp;&amp; car!=&#8217;3&#8242; &amp;&amp; car!=&#8217;4&#8242; &amp;&amp; car!=&#8217;5&#8242;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; car!=&#8217;6&#8242; &amp;&amp; car!=&#8217;7&#8242; &amp;&amp; car!=&#8217;8&#8242; &amp;&amp; car!=&#8217;9&#8242; &amp;&amp; car!=&#8217;.&#8217;)){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getToolkit().beep();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; evt.consume();<br \/>&nbsp;}<\/span><\/span><\/div>\n<\/div>\n<p>Este c\u00f3digo se agrega en el evento siguiente del jtextfield:<\/p>\n<p><span style=\"color: #660000;\">private void jtxtPesoKeyTyped(java.awt.event.KeyEvent evt) {<\/span><\/p>\n<p><span style=\"color: #660000;\">}<\/span><\/p>\n<p>Y espero sus comentarios<\/p>\n<div style=\"text-align: justify;\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A veces es necesario validar campos de texto de tal modo que el usuario cometa el m\u00ednimo error posible, ya que siempre es posible que lo haga. Recientemente necesit\u00e9 validar unos jtextfield, en donde soloa aceptaban n\u00fameros decimales, pero no reduje el c\u00f3digo siguiente: Si alguien conoce alg\u00fan m\u00e9tod mucho menor, favor de comentar: if(jtxtPeso.getText().length() [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[733,738,446,734,737,735,736],"tags":[],"class_list":["post-1634","post","type-post","status-publish","format-standard","hentry","category-aceptar-digitos-numericos-y-un-solo-punto","category-decimal","category-java","category-jtextfield","category-numero-decimal","category-validar","category-validate"],"_links":{"self":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts\/1634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":0,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"wp:attachment":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}