我有一个DAOImplementation类,其方法定义如下。
@Override
public Registration getRegistrationInfoById(int aRegistrationId) {
String SQL = "{CALL getRegistrationInfoById(?)}";
Registration aRegistration = new Registration();
try (Connection con = DBUtil.getConnection(DBType.MYSQL);
CallableStatement cs = con.prepareCall(SQL);) {
cs.setInt(1, aRegistrationId);
try (ResultSet rs = cs.executeQuery();) {
while (rs.next()) {
int gradeLevel = Integer.parseInt(rs.getString(RegistrationTable.GRADELEVEL));
aRegistration.setGradeLevel(gradeLevel);
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getErrorCode() + "\n" + e.getMessage());
}
return aRegistration;
}//end of method
这将返回我已验证的Grade Level整数值(1,2,3,4,5,6,7等),因为我尝试打印
aRegistration.getGradeLevel();
返回的输出现在我的问题是我的
JComboBox
。我为我的ListCellRenderer
设置了一个JComboBox
,其中包含所有GradeLevel
值public class JComboBoxRenderer_GradeLevel extends JLabel implements ListCellRenderer<Object> {
public JComboBoxRenderer_GradeLevel() {
this.setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
} else {
this.setText("--");
}
if (isSelected) {
this.setBackground(Color.YELLOW);
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
}
}
看起来像这个
JComboBox
一样。 (渲染GradeLevel
模型以仅显示gradelevel的int值),((GradeLevel) value).getGradelevel());
返回一个整数值。我了解即使
JComboBox
的渲染器通过使用GradeLevel
显示((GradeLevel)value).getGradeLevel()
的整数值,但JComboBox
上的实际值仍被视为GradeLevel
或对象的实例。但不是String
或int
。所以我的问题是,当我尝试将所选值设置为int值时,它不会更改
JComboBox
的所选值。当我使用setSelectedItem();
时没有任何反应这就是我试图为GUI所做的。
//Grade Level
GradeLevelDaoImpl gldi = new GradeLevelDaoImpl();
List<GradeLevel> gradeLevels = gldi.getAllGradeLevelsInfo();
DefaultComboBoxModel gradeLevelModel = new DefaultComboBoxModel(gradeLevels.toArray());
jcmbGradeLevel.setModel(gradeLevelModel);
jcmbGradeLevel.setRenderer(new JComboBoxRenderer_GradeLevel());
jcmbGradeLevel.setSelectedIndex(-1);
GradeLevel gradeLevel = new GradeLevel();
gradeLevel.setGradelevel(registration.getGradeLevel());
jcmbGradeLevel.setSelectedItem(gradeLevel); //PROBLEM HERE, it doesn't change
JOptionPane
显示此内容。JOptionPane.showMessageDialog(null,"GradeLevel: "+gradeLevel);
JOptionPane.showMessageDialog(null,"GradeLevel: "+gradeLevel.getGradeLevel());
它似乎无法将我尝试将其设置为(
gradeLevel
)的对象与对象JComboBox
具有(gradeLevels
)的对象进行比较。注意单数和复数。如何操作类型,使
setSelectedItem()
与JComboBox
的类型匹配?谢谢。
最佳答案
如果要通过使用对象的不同实例但具有相同的属性来执行此操作,则需要重写类的equals
和hashcode
方法,以便属性的组合是唯一的。这非常重要,这是一种关系期望,即任何equal
与另一个对象都将具有相同的hashcode
这是一个非常简单的示例,我在IDE的自动生成过程中使用了它(因为我很懒),但是,如果您的Registration
类具有与该类实例进行比较时应考虑的其他属性,则需要修改它来支持他们(同样,任何好的IDE都应该能够做到这一点)
public class Registration {
private int gradeLevel;
public Registration(int gradeLevel) {
this.gradeLevel = gradeLevel;
}
public int getGradeLevel() {
return gradeLevel;
}
public void setGradeLevel(int gradeLevel) {
this.gradeLevel = gradeLevel;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + this.gradeLevel;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Registration other = (Registration) obj;
if (this.gradeLevel != other.gradeLevel) {
return false;
}
return true;
}
}
然后使用类似...
Registration a = new Registration(1);
Registration b = new Registration(1);
Registration c = new Registration(2);
System.out.println(a.equals(b));
System.out.println(a.equals(c));
System.out.println(b.equals(c));
将打印...
true
false
false
向我们显示该代码正在运行。
一旦完成此设置,您就可以通过创建
Registration
实例,为它添加所需的属性并将其传递给JComboBox
来更改所选项目。这是非常重要的,并且在Java中经常使用的非常普遍的概念,值得花时间学习和理解