如何检查JTextField中是否包含某些内容?

时间:2014-02-19 21:44:50

标签: java swing

我正在进行状态检查,如果我的所有10个JTextField都被填写,我想要运行一些代码。我怎么能这样做?谢谢。这是在java

4 个答案:

答案 0 :(得分:2)

我认为你需要循环遍历所有字段并调用field.getText()并检查return!= null和长度> 0

答案 1 :(得分:2)

将所有JTextField放入数组或ArrayList<JTextField>并遍历列表检查:

// assuming an ArrayList of JTextField called fieldList:
boolean filled = true;
for (JTextField field : fieldList) {
  if (field.getText().trim().isEmpty()) {
    filled = false;
  }
}

// if filled true, then all fields are filled

答案 2 :(得分:0)

如果所有字段都在列表中

int emptyIndex = -1;
for(int i = 0; i < list.size(); i++){
   String text = list.get(i).getText();
   if(text.length() == 0){
       emptyIndex = i;
   }
}

if(emptyIndex == -1)
  //there is no empty text fields
else
 // the empty text field is list.get(emptyIndex)

答案 3 :(得分:0)

将所有JTextField放入数组或HashMap<JTextField,Boolean>并遍历列表检查:

// assuming an ArrayList of JTextField called fieldList and the boolean values are by default true:
boolean filled = true;
for (JTextField field : fieldList) {
  if (field.getText().trim().equals("")) {
      filled = false;
      fieldList.put(field, new Boolean(false));
 }
}

// you can complete the code here to process the empty field after you have detected each one