java - JComboBox setSelectedItem 不起作用

标签 java swing jpanel jcombobox

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

我为文本框设置了值,但我不明白为什么 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;
    }

最佳答案

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

例如,年份是从 1900 年到 1990 年的 String,但是如果我提供值 72,则组合框中没有匹配值可以匹配.

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

您有多种选择...

你可以……

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

这将使您的辅助代码看起来更像...

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

关于java - JComboBox setSelectedItem 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16756367/

相关文章:

java - Spring Security ACL继承: How does it really works?

java - 设置等效于 WeakHashMap?

java - 为什么 setPreferredSize 不改变按钮的大小?

java - 当用户单击并拖动时调整 Path2D 形状的大小

java - 我如何创建两个具有相同Columnmodel但具有不同模型的Jtable

java - 按钮方法突然停止工作

java - 如何在kotlin中获取当前类文件名

java - 在 Border 和 JFrame 之间留出间隙

java - JTable 如何触发选择一行或双击一行的事件

java - 如何在 Java 中隐藏面板