Java 无法将对象实例设置为带有渲染器的JComboBox的选定项
我有一个DAO实现类,其方法定义如下Java 无法将对象实例设置为带有渲染器的JComboBox的选定项,java,swing,jcombobox,listcellrenderer,Java,Swing,Jcombobox,Listcellrenderer,我有一个DAO实现类,其方法定义如下 @Override public Registration getRegistrationInfoById(int aRegistrationId) { String SQL = "{CALL getRegistrationInfoById(?)}"; Registration aRegistration = new Registration(); try (Connection con = DBUti
@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
。我为我的JComboBox
设置了一个ListCellRenderer
,它保存所有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;
}
}
JOptionPane
显示此选项
JOptionPane.showMessageDialog(null,"GradeLevel: "+gradeLevel);
它似乎无法将我试图将其设置为(gradeLevel
)的对象与JComboBox
拥有的对象(gradeLevels
)进行比较。注意单数和复数
如何操作类型,使setSelectedItem()
与JComboBox
的内容相匹配
谢谢。如果您想使用对象的不同实例,但具有相同的属性,那么您需要重写类的equals
和hashcode
方法,以便属性的组合是唯一的。这一点非常重要,这是一种关系期望,即与另一个对象相等的任何对象都将具有相同的hashcode
这是一个非常快速的示例,我在IDE的自动生成过程中使用了它(因为我很懒),但是,如果您的Registration
类有其他属性,在与类的实例进行比较时应该考虑这些属性,您将需要修改它以支持它们(同样,任何好的IDE都应该能够做到这一点)
然后使用类似于
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
这向我们展示了代码正在工作
获得此设置后,您应该能够通过创建注册
的实例来更改所选项目,为其设置所需属性的种子,并将其传递到JComboBox
这是Java中经常使用的非常重要且非常常见的概念,非常值得花时间学习和理解如果您想使用对象实现这一点,您需要重写类的(注册
)equals
和hashcode
方法,因此,任何具有gradeLevel
的1
的对象都将相等,依此类推forth@MadProgrammer我试着在equals和hashcode覆盖上快速搜索一下。所以我得到的是,我只需要在注册课上做这个D是吗?还在读我在谷歌上搜索的信息。以前没听说过它。@p3ace,我只需要在注册类上执行它?
-在添加到组合框的对象类上实现这些方法。如果您的渲染器需要GradeLevel
对象,那么这就告诉我您应该在GradeLevel
类中实现这些方法。@camickr哦,是的,对不起,我在考虑另一个类。关于下一步该做什么,有点混乱PObject#equals
(和Object#hashcode
)是Java中一个非常常见和广泛使用的概念,它基本上表示,虽然objA!=objB
因为它们是两个不同的实例objA.equals(objB)
可以返回true
如果它们包含相同的属性(由开发人员定义),我仍在努力吸收这些信息。我可能要花点时间来吸收。会接受你的建议,会学到这一点。我很欣赏这个例子。示例总是帮助我更容易理解新概念。谢谢
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