JComboBox setSelectedItem不起作用

问题描述 投票:3回答:5

我试图在填充组合框之后在我的setSelectedItem类的构造函数中设置JComboBoxJPanel

我设置了文本框的值,但我无法弄清楚为什么setSelectedItem似乎不起作用。有任何想法吗?

public StudentProfilePanel(StudentInfo si) {

        yesButton.setBounds(50, 346, 69, 40);
        noButton.setBounds(121, 346, 56, 40);
        this.add(yesButton);
        this.add(noButton);
        setLayout(null);
        comboBoxYear.setModel(new DefaultComboBoxModel(years()));
        comboBoxYear.setBounds(202, 365, 62, 23);
        if(si.birthdate!=null){
            //System.out.println("year value : ["+dateofbirth(si.birthdate)[2]+"]");
            comboBoxYear.setSelectedItem(dateofbirth(si.birthdate)[2]);

        }

        add(comboBoxYear);
        comboBoxMonth.setModel(new DefaultComboBoxModel(new String[]{"01","02","03","04","05","06","07","08","09","10","11","12"}));
        comboBoxMonth.setBounds(285, 365, 56, 23);

        //set month value
        if(si.birthdate!=null){
            //comboBoxMonth.setSelectedItem(dateofbirth(si.birthdate)[1]);
            comboBoxMonth.setSelectedItem("04");
            System.out.println("month value : ["+dateofbirth(si.birthdate)[1]+"]");
        }
        add(comboBoxMonth);
        comboBoxDay.setModel(new DefaultComboBoxModel(days()));
        comboBoxDay.setBounds(351, 365, 54, 23);
        if(si.birthdate!=null){
            //comboBoxDay.setSelectedItem(dateofbirth(si.birthdate)[0]);
            comboBoxDay.setSelectedItem(dateofbirth(si.birthdate)[0]);
        }
        add(comboBoxDay);

        textFieldFirstName = new JTextField();
        textFieldFirstName.setBounds(21, 321, 171, 21);
        add(textFieldFirstName);
        textFieldFirstName.setColumns(10);
        // set the value of first name
        textFieldFirstName.setText(si.firstName);

        textFieldLastName = new JTextField();
        textFieldLastName.setBounds(242, 321, 163, 21);
        add(textFieldLastName);
        textFieldLastName.setColumns(10);
        //set the value of the last name
        textFieldLastName.setText(si.lastName);

        JPanel panelPersonPhoto = new ImagePanel(
                "C:\\Users\\MDJef\\Pictures\\Wallpaper\\General\\11.jpg");
        panelPersonPhoto.setBorder(new TitledBorder(null, "",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panelPersonPhoto.setBounds(21, 20, 384, 291);
        add(panelPersonPhoto);
    }

非常感谢。

我使用的辅助方法

    // jf : helper method
    public String[] years() {
        String[] results = new String[90];
        for (int i = 0; i < 90; i++) {
            results[i] = Integer.toString(1900 + i);
        }
        return results;
    }

    // jf : helper method
    public String[] months() {
        String[] results = new String[12];
        for (int i = 0; i < 12; i++) {
            results[i] = Integer.toString(i + 1);
        }
        return results;
    }

    // jf : helper method
    public String[] days() {
        String[] results = new String[31];
        for (int i = 0; i < 31; i++) {
            results[i] = Integer.toString(i + 1);
        }
        return results;
    }

    // jf : helper method
    public String[] dateofbirth(String dob) {
        String[] tokens = dob.split("-");
        return tokens;
    }
java swing jpanel jcombobox
5个回答
2
投票

分配给组合框的值与您尝试设置的值不同。

例如,年份是1900年至1990年的Strings,但是如果我提供值72,则组合框中没有匹配的匹配值。

同样,你的daysmonths方法只返回未填充的值(即01),其中,在你的代码中,你试图使用填充值(即04)设置值,这意味着没有匹配的值...

你有很多选择......

你可以...

将所有值转换为int,这意味着组合框中的值只是ints。然后,您还需要将日期值转换为ints。

这会让你的帮助代码更像......

public int[] years() {
    int[] results = new String[90];
    for (int i = 0; i < 90; i++) {
        results[i] = 1900 + i;
    }
    return results;
}

public int[] months() {
    int[] results = new String[12];
    for (int i = 0; i < 12; i++) {
        results[i] = i + 1;
    }
    return results;
}

public int[] days() {
    int[] results = new String[31];
    for (int i = 0; i < 31; i++) {
        results[i] = i + 1;
    }
    return results;
}

public int[] dateofbirth(String dob) {
    int[] tokens = dob.split("-");
    int[] values = new int[tokens.length];
    for (int index = 0; index < tokens.length; index++) {
      values[index] = Integer.parse(tokens[index]);
    }
    return index;
}

更好的解决方案

将使用JSpinner,它将自动处理日期滚动问题和验证。

看看Using Standard Spinner Models and Editors


1
投票

与您的问题无关,但是:

yesButton.setBounds(50, 346, 69, 40);
noButton.setBounds(121, 346, 56, 40);
setLayout(null);

不要使用null布局和setBounds(...)。 Swing旨在与布局管理器一起使用。从长远来看,您将节省时间。

if(si.birthdate!=null){

不要直接访问类中的变量。创建一个getter方法来访问类的属性。

//System.out.println("year value : ["+dateofbirth(si.birthdate)[2]+"]");
comboBoxYear.setSelectedItem(dateofbirth(si.birthdate)[2]);

不要总是试图将代码强制转换为单个语句。而是做类似的事情:

String birthdate = dateofbirth(si.birthdate[2]);
System.out.println("year value : [" + birthdate +"]");
comboBoxYear.setSelectedItem(birthdate);

这有助于您的调试,因为现在您知道您显示的变量与您尝试在setSelectedItem()方法中使用的变量相同。它可以节省两次输入语句,避免输入错误。


1
投票

当你调用comboBoxMonth.setSelectedItem("04");时,你试图选择一个新创建的String,它不等于JComboBox中的那个。因此它没有被选中。

您可以尝试这样的事情:

String[] months = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
comboBoxMonth.setModel(new DefaultComboBoxModel(months));

comboBoxMonth.setSelectedItem(months[3]);

编辑:试试这个。它使用项目的索引代替。只需确保为数组添加月份。

String[] months = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
comboBoxMonth.setModel(new DefaultComboBoxModel(months));

if(si.birthdate!=null)
{
    comboBoxMonth.setSelectedIndex(Integer.parseInteger(dateofbirth(si.birthdate)[1]) - 1);
}

0
投票

对于具有相同问题的其他开发人员:仔细研究setSelectedItem(Object anObject)JComboBox的实现可能会有所帮助:

public void setSelectedItem(Object anObject) {
    Object oldSelection = selectedItemReminder;
    Object objectToSelect = anObject;
    if (oldSelection == null || !oldSelection.equals(anObject)) {

        if (anObject != null && !isEditable()) {
            // For non editable combo boxes, an invalid selection
            // will be rejected.
            boolean found = false;
            for (int i = 0; i < dataModel.getSize(); i++) {
                E element = dataModel.getElementAt(i);
                if (anObject.equals(element)) {
                    found = true;
                    objectToSelect = element;
                    break;
                }
            }
            if (!found) {
                return;
            }
        }

...

在循环中,您的对象将与具有特定类型E的dataModel对象进行比较。在从String实现equals()时,您可以看到类/接口,长度和每个字符的验证。这意味着,我们的对象必须具有相同的类型,并且所有字符必须相同!

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

这是if (anObject.equals(element))中最烦人的部分setSelectedItem!你不能从你的元素重写equals方法。例如StudentInfo并比较其他类型,如字符串或整数。简单的例子。你实现像这个JComboBox<StudentInfo>这样的组合框,你想选择int id = 2;的学生。所以它现在比较IntegerStudentInfo。在这里你必须从Integer覆盖等于...

我的建议是交换它。创建自己的类,添加boolean selectingItem并覆盖setSelectedItem(Object anObject)contentsChanged(ListDataEvent e)(此方法一对一)。不过,我在一个项目中有副作用......


0
投票

使用以下:comboBoxMonth.setSelectedItem(index of the array);

© www.soinside.com 2019 - 2024. All rights reserved.