简体   繁体   English

如何重置JTextField…?

[英]How can I reset JTextField…?

I used the below pgm to save the details on address book. 我使用以下pgm将详细信息保存在地址簿中。 I want to check the mob filed whether it is an int or not. 我想检查是否是int的暴民。 It will execute properly at the first time (If i enter letter in mob field it will not save). 它将在第一次正确执行(如果我在mob字段中输入字母,将不会保存)。 but from the 2nd time on words it will save even if i entered a letter in the mob filed. 但是从第二次开始,即使我在提交的暴民中输入了一封信,它也会保存下来。 mob will store the previous value. mob将存储先前的值。 How can i clear the mob filed after executing the first time..... 我如何在第一次执行后清除暴徒提交的文件.....

 public void savePerson() {  
    name   =   jtfName.getText();
    name   =   name.toUpperCase();  
    jtfName.setText(name);
    address = jtfAddress.getText();

    try {
        landline = Integer.parseInt(""+jtfLandline.getText());
    } catch(Exception e) {          
    }

    try {           
       mob = Integer.parseInt(""+jtfmob.getText());     
    }catch(Exception e) {
    }

    email   = jtfEmail.getText();
   if(name.equals("")) {
        JOptionPane.showMessageDialog(null, "Please enter person name.");           
   } else if(mob == 0) {
        JOptionPane.showMessageDialog(null, "Please enter Mobile Number.");
   } else {
          //create a PersonInfo object and pass it to PersonDAO to save it
          PersonInfo person = new PersonInfo(name, address, landline, mob , email);
          pDAO.savePerson(person);
          JOptionPane.showMessageDialog(null, "Person Saved");            
          clear();         
   }
 }

在下次执行之前,请使用方法jtfmob.setText("") ,然后编写完整的逻辑。

Try out this, 试试这个

Once the first iteration is over, reset your text field by setting like below 第一次迭代结束后,通过如下设置重置您的文本字段

jtfmob.setText("");

To get the number inputs alone, try the following codes 要单独获取数字输入,请尝试以下代码

jtfmob = new JFormattedTextField(NumberFormat.getInstance());

This is very obvious case. 这是非常明显的情况。 When you parse the jtfmob text field first time, It will look for the value in it .. If the value is not integer, it will not be parsed at all and you will get null value. 首次解析jtfmob文本字段时,它将在其中查找值。如果该值不是整数,则将完全不对其进行解析,并且您将获得空值。 But if the value is integer you would be proceeded without any problem. 但是,如果该值是整数,则可以继续进行而不会出现任何问题。

mob = Integer.parseInt(""+jtfmob.getText());

mob will contain the parsed value in integer data type . mob将包含整数数据类型的解析值。 BUT next time when you put letter in text field, it wouldnot be parsable and hence cause an exception (which you are catching successfully).The control of program would be transferred to catch block without updating the value of Integer type variable mob . 但是下次当您在文本字段中输入字母时,它将无法解析并因此导致异常(成功捕获)。程序的控制权将转移到catch块,而不会更新Integer类型变量mob的值。 so mob will contain the OLD value (because value is not updated because of exception) and same you are getting in this case . 所以mob将包含OLD值(因为由于异常而未更新值),在这种情况下您将得到OLD值。

To solve this problem either show some message in catch block to enter right value or find some other way by yourself 要解决此问题,请在catch块中显示一些消息以输入正确的值,或者自己寻找其他方法

Use the following in your constructor. 在构造函数中使用以下内容。

jTextField1.setText("");

It will reset your values each time before you click the button. 每次单击按钮之前,它将重置您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM