java-JComboBox setSelectedItem不起作用
作者:互联网
我试图在填充组合框后在我的JPanel类的构造函数中设置JComboBox的setSelectedItem.
我为文本框设置了值,但是我不知道为什么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年的字符串,但是如果我提供值72,则组合框中没有匹配的值可匹配.
同样,您的days和months方法仅返回未填充的值(即01),因为在您的代码中,您尝试使用填充的值(即04)来设置值,这意味着没有匹配的值…
您有很多选择…
你可以…
将所有值转换为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
标签:jcombobox,swing,jpanel,java 来源: https://codeday.me/bug/20191030/1971105.html