2016-03-29 24 views
2

Kullanıcının varsayılan öğe dışındaki herhangi bir öğeyi seçmesi için jComboBox nasıl doğrulanır?ComboBox için hazırda bulunan doğrulayıcı

Kullanıcının herhangi bir öğe seçip seçmediğini kontrol etmem gerekiyor. Yani JComboBox değeri modelim sınıfta

enter image description here

, "Bir scurity soru seçin" olacak doğrulamak için getSeqQue() eklemek için hazırda beklet doğrulayıcı ek açıklama ne olacak
@NotEmpty(message="Please fill the username field!") 
public String getUsername() { 
    return this.username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

@NotEmpty(message="Please fill the password field!") 
public String getPassword() { 
    return this.password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public String getSeqQue() { 
    return this.seqQue; 
} 

public void setSeqQue(String seqQue) { 
    this.seqQue = seqQue; 
} 

benim JComboBox?

cevap

1

Özel mesajla JComboBox'unuzu doğrulamak için özel kısıtlama doğrulayıcı yapabilirsiniz.

aşağıdaki örneğe bakın:

MyModel.java

public class MyModel { 

    @ValidComboBox //this is the annotation which validates your combo box 
    private String question; 

    //getter and setter 
} 

ValidComboBox.java // açıklama

import java.lang.annotation.*; 
import javax.validation.*; 

@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE }) 
@Retention(RetentionPolicy.RUNTIME) 
@Constraint(validatedBy = ComboBoxValidator.class) 
@Documented 
public @interface ValidComboBox { 
String value = "Please select a security question"; 

String message() default "Please select a security question."; 

Class<?>[]groups() default {}; 

Class<? extends Payload>[]payload() default {}; 
} 

ComboBoxValidator.java

import javax.validation.*; 
public class ComboBoxValidator implements ConstraintValidator<ValidComboBox, String> { 

private String value; 

@Override 
public void initialize(ValidComboBox arg0) { 
    this.value = arg0.value; 

} 

@Override 
public boolean isValid(String question, ConstraintValidatorContext arg1) { 
    if(question.equalsIgnoreCase(value)){ 
     return false; 
    } 
    return true; 
} 
} 

bu gibi JComboBox öğe ekleme

JComboBox<String> jComboBox = new JComboBox<>(); 
jComboBox.addItem("Please select a scurity question"); 
jComboBox.addItem("Question 1"); 
jComboBox.addItem("Question 2"); 

Ve aşağıdaki satırları doğrulamaya eylemi gerçekleştirdiğinizde eklemek gerekir:

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); 
Validator validator = validatorFactory.getValidator(); 

String question = jComboBox.getSelectedItem().toString(); 
MyModel model = new MyModel(); 
model.setQuestion(model); 

Set<ConstraintViolation<MyModel>> constraintViolations = validator.validate(model); 

if (!constraintViolations.isEmpty()) { 
     String error = ""; 
     for (ConstraintViolation<MyModel> constraintViolation : constraintViolations) { 
       error += constraintViolation.getMessage(); 
       JOptionPane.showMessageDialog(null, error); 
     } 
} 

O gösterecektir Soru seçmeden istek göndermeye çalıştığınızda lütfen bir güvenlik sorusu mesajını seçin.

İlgili konular