我正在设计基于DAO Pattern的类。
我有3个班级和1个GUI表格。
public interface SchoolYearDao {
List<SchoolYear> getAllSchoolYearInfo();
List<SchoolYear> getAllSchoolYearStart();
List<SchoolYear> getAllSchoolYearEnd();
List<SchoolYear> getSchoolYearById(int aSchoolYearId);
int getSchoolYearId(SchoolYear schoolyear);
boolean addSchoolYear(SchoolYear schoolyear);
}
public class SchoolYear {
//setters and getters
}
public class SchoolYearDaoImpl implements SchoolYearDao{
@Override
public List<SchoolYear> getAllSchoolYearStart() {
List<SchoolYear> listOfSchoolYearStart = new ArrayList<>();
SchoolYear mySchoolYear = new SchoolYear();
String SQL = "{CALL getAllSchoolYearInfo()}";
try(Connection con = DBUtil.getConnection(DBType.MYSQL);
CallableStatement cs = con.prepareCall(SQL);) {
try(ResultSet rs = cs.executeQuery();){
while(rs.next()){
mySchoolYear.setStart(rs.getInt("yearFrom"));
}
listOfSchoolYearStart.add(mySchoolYear);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e.getMessage());
}
System.out.println(listOfSchoolYearStart);
return listOfSchoolYearStart;
}
}
问题出在GUI上。
public class SchoolYearGUI extends javax.swing.JPanel {
public SchoolYearGUI() {
initComponents();
schoolYearStartJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearInfo().toArray());
schoolYearEndJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearEnd().toArray()));
}
}
我无法正确显示岁月。我明白了
而不是实际的整数2015,2016,2017等......
我在线研究并发现了类似的问题,但大部分都没有使用class
列表作为List<SchoolYear>
。在这种情况下,“SchoolYear”是类的名称。
我使用toArray();
并尝试了Arrays.toString(array);
但无法正确使用DefaultComboBoxModel
。
我以为我会将返回类型更改为getAllSchoolYearStart()
方法的List<SchoolYear>
,但我意识到我必须将JTables
保留为返回类型,以防我需要使用结果集作为List<SchoolYear>
等的模型。
所以,我想坚持使用MadProgrammer
作为返回类型。 (如果这是个好主意?)
获得实际价值的最佳方法是什么?
提前致谢。
===============解决方案============================== < / p>
感谢listcellrenderer
提供建议和其他回复者。
所以我一夜之间研究了public MainFrame() {
initComponents();
SchoolYearDaoImpl sy = new SchoolYearDaoImpl();
DefaultComboBoxModel model = new DefaultComboBoxModel(sy.getAllSchoolYearStart().toArray());
jcmbSchoolYearStart.setModel(model);
jcmbSchoolYearStart.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(value instanceof SchoolYear){
SchoolYear schoolyear = (SchoolYear) value;
setText(""+schoolyear.getStart());
}
return this;
}
} );
}
,最后得到了如何使用它的基本思路。
getListCellRendererComponent
我重写了if-statement
并创建了一个value
来测试我的类的实例是否为“SchoolYear”然后我将所有原始getStart()
转换为 SchoolYear 然后使用SchoolYear模型的getter rmagick
来获取存储在列表中的值。
我现在正在将渲染器移动到项目中的外部类文件中。
答案 0 :(得分:1)
正如@MadProgrammer所说,在new DefaultComboBoxModel(schoolyear.getAllSchoolYearInfo().toArray()
中,你输入了一个Object
的数组,JComboBox
的构造函数会尝试使用toString()
方法转换SchoolYear
的每个实例以将其显示为纯文本。如果您没有覆盖默认的toString()
方法以按照您的喜好展示它,您将看到您在现在的组合框中看到的内容:该类的名称带有一些数字。
您可以实施toString()
方法,但这不是最好的方法。您可以构造一些实用方法,以及您拥有的getSchoolYearId()
,以获取List中每个对象的ID,并使用ID填充数组。
private int[] getIDAndFillAnArray(List<SchoolYear> syrs) {
int[] ids = new int[syrs.size()];
for (int i=0; i<syrs.size(); i++) {
ids[i] = syrs.get(i).getSchoolYearId();
}
return ids;
}
就像使用它一样:
schoolYearStartJcbx.setModel(new DefaultComboBoxModel(getIDAndFillAnArray(schoolyear.getAllSchoolYearInfo()));
这很简单。
答案 1 :(得分:0)
使用此课程:
public class ComboItem {
private String value;
private String label;
public ComboItem(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return this.value;
}
public String getLabel() {
return this.label;
}
@Override
public String toString() {
return label;
}
}
将此方法放在连接数据库的位置:
public ComboItem[] getListOfSchoolYearStart(params..)
{
List<ComboItem> result = new ArrayList<ComboItem>();
ComboItem[] items;
.....
rs = stmt.executeQuery(query);
while (rs.next()) {
ComboItem item = new ComboItem(rs.getInt("id_school") + "", rs.getString("description"));
result.add(item);
}
items = result.toArray(new ComboItem[result.size()]);
return items;
}
添加您的JComboBox:
private ComboItem[] listOfSchoolYearStart;
private int selectedIdSchool=-1;
....
listOfSchoolYearStart= getListOfSchoolYearStart();
JComboBox comboList = new JComboBox(listOfSchoolYearStart);
//If you want to keep previous selection
if (listOfSchoolYearStart.length > 0)
{
boolean isFound=false;
for (ComboItem comb : listOfSchoolYearStart) {
if(Integer.parseInt(comb.getValue())==selectedIdSchool)
{
comboList.setSelectedItem(comb);
isFound=true;
break;
}
}
if(!isFound)
{
comboList.setSelectedIndex(0);
selectedIdSchool=Integer.parseInt(listOfSchoolYearStart[0].getValue());
}
}
这至少对我有用,我希望它有所帮助。