验证jframe组合框,以便不接受默认值

时间:2015-01-08 23:55:56

标签: java validation combobox jcombobox

我正在尝试确保这些组合框没有默认值。

这是我到目前为止验证它的代码

public boolean checked() {

    /*this code loops through all comboboxes, checks they are not default 
     * and adds 1 to i. if i is 6, by the end of then it means that none
     * are default and true is returned
     */

    int i = 0;

    if(!drop1.equals("Year")){
        i++;    
    }
    if (!drop2.equals("Month")) {
        i++;   
    }
    if (!drop3.equals("Day")) {
        i++;
    }
    if (!drop4.equals("Year Group")) {
        i++;
    }
    if (!drop5.equals("Year")) {
        i++;
    }
    if (!drop6.equals("Year")) {
        i++;
    }

    if (i == 6) {
        return true;
    } else {
        return false;
    }

}

dropX是单独的下拉框

1 个答案:

答案 0 :(得分:0)

在查看了jcombobox的类方法之后,我想出了这个:

public boolean checked(){

    /*this code loops through all comboboxes, checks they are not default 
     * and adds 1 to i. if i is 6, by the end of then it means that none
     * are default and true is returned
     */
    int i = 0;
    if(drop1.getSelectedIndex() != 0){
        i++;    
    }
    if(drop2.getSelectedIndex() != 0){
        i++;   
    }
    if(drop3.getSelectedIndex() != 0){
        i++;
    }
    if(drop4.getSelectedIndex() != 0){
        i++;
    }
    if(drop5.getSelectedIndex() != 0){
        i++;
    }
    if(drop6.getSelectedIndex() != 0){
        i++;
    }

    if(i == 6){
        return true;
    }else{
        return false;
    }

}

getSelectedIndex()确定组合框的状态。如果组合框是默认值(值0),那么我永远不会达到6。