2013-04-21 39 views
0

驗證swing應用程序的輸入字段(如文本字段,組合框等)的最佳方式是什麼,並讓用戶只有在一切正常時才按下Save按鈕。假設搜索功能也在同一個界面中。因此搜索記錄也會填寫輸入字段。但在這種情況下保存按鈕應保持禁用狀態。只有驗證爲真時才啓用Jbutton

initComponents(); 
     btnSave.setEnabled(false); 

     txt1.getDocument().addDocumentListener(new DocumentListener() { 
       @Override 
       public void changedUpdate(DocumentEvent e) { 

       } 

       @Override 
       public void removeUpdate(DocumentEvent e) { 
        validate(txt1.getText(),e); 
       } 

       @Override 
       public void insertUpdate(DocumentEvent e) { 
        validate(txt1.getText(),e); 
       } 

       public void validate(String enteredText,DocumentEvent e) { 
        String currText = ""; 
        try { 
          Document doc = (Document) e.getDocument(); 
          currText = doc.getText(0, doc.getLength()); 
        } catch (BadLocationException e1) { 
        } 

        if(enteredText.equals(currText)){ 
         //if validated successfully 
          btnSave.setEnabled(false); 
        }else{ 
        btnSave.setEnabled(true); 
        } 
       } 
     }); 
+0

@vishal_aim我在表單加載時設置了enable false。然後在驗證方法結束時將保存按鈕設置爲true。然後我在最後一個文本字段的按下事件中調用了Validate方法。 – amal 2013-04-21 12:20:53

+0

如果用戶填寫了最後一個文本字段並清除了以前的文本字段之一,該怎麼辦? 然後,您需要將該操作添加到所有輸入元素。 – 2013-04-21 12:23:46

+0

@GnomezGrave。這就是我在這裏的問題。我會嘗試你的建議。 – amal 2013-04-21 12:27:04

回答

1

你試過這樣嗎?

final JTextField textField = new JTextField(); 
    final JButton submitBtn = new JButton(); 
    submitBtn.setEnabled(true); 

    textField.getDocument().addDocumentListener(new DocumentListener() { 
     public void changedUpdate(DocumentEvent e) { 
      validate(e); 
     } 
     public void removeUpdate(DocumentEvent e) { 
      validate(e); 
     } 
     public void insertUpdate(DocumentEvent e) { 
      validate(e); 
     } 

     public void validate(String enteredText) { 
      String currText = ""; 
      try { 
       Document doc = (Document)e.getDocument(); 
       currText = doc.getText(0, doc.getLength()); 
      } catch (BadLocationException e1) { 
       e1.printStackTrace(); 
      } 
      //validation of currText here 

      //if validated successfully 
      submitBtn.setEnabled(true); 
      //else 
      submitBtn.setEnabled(false); 
     } 
    }); 
+0

Thnx爲你的答覆。不,我沒有嘗試過這樣的事情。我會試試這個,讓你知道。 – amal 2013-04-21 12:38:27

+0

不錯的主意,但我不喜歡這樣,[請看正確的(我的看法),因爲字符串可以從ClipBoard插入,或先前選定的文本可以刪除](http://stackoverflow.com/questions/8572948/java-jformattedtextfield-saving-numbers/8582656#8582656) – mKorbel 2013-04-21 12:44:16

+0

@vishal_aim我試着用你給我的代碼。但它不適合我。對不起,我仍然是java初學者。你能看看我的代碼嗎?我更新了原帖 – amal 2013-04-21 13:30:06

0

創建一個方法來檢查所有的輸入是完成還是/和所有驗證都通過並最終返回一個布爾值。

public boolean validate(...){ 
    //some stuff 
    if(validated){ 
    return true; 
    }else{ 
    return false; 
    } 
} 

然後你就可以使用它。

button.setEnabled(validate(...)); 
+0

Thanx爲您的迴應。我已經嘗試過這樣的事情。但沒有嘗試這樣。 button.setEnabled(驗證(...));讓你很快知道 – amal 2013-04-21 12:25:16

1

條件使用setEnabled()保存按鈕在兩個地方enabled屬性:

  • 在您在連接到每個相關組件的InputVerifier實施shouldYieldFocus()。本教程和一些示例被引用爲here

  • 在你的組件的普通監聽器中。

相關問題