我的數據庫中有兩張表作爲學期表和課程表。學期表中有semesterId,courseId,courseName和Sdepartment(部門名稱)。課程表中有courseId和courseName。 我有兩個組合框我的jframe.First是爲選擇一個部門。第二個是選擇課程。我想選擇課程作爲選定的部門。 當我選擇一個部門時,如何在組合框中調用課程名稱? 這裏我的代碼;如何在java中的組合框之間使用過濾器?
public void coursename(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
//Query query= session.createQuery("select a.courseName,e.semesterId from Semester e inner join e.course as a");
Query query= session.createQuery("FROM Senior.entity.Semester S ");
//for (Iterator it = query.iterate(); it.hasNext();) {
//Object row[] = (Object[]) it.next();
//combocourse.addItem(new CourseItem((String)row[0], (int)row[1]));
//}
List <Semester>re= query.list();
if (re.size() > 0){
Iterator iterate= re.iterator();
final Semester resultAccount= (Semester)iterate.next();
combocourse.removeAllItems();
for(Semester inv:re){
combocourse.addItem(new CourseItem(inv.getSemesterId(),inv.getSCourse()));
}
}
session.close();
}
public void depart(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query= session.createQuery("FROM Senior.entity.Semester f ");
List <Semester>results= query.list();
if (results.size() > 0){
Iterator iterate= results.iterator();
final Semester resultAccount= (Semester)iterate.next();
combodepart.removeAllItems();
for(Semester inv:results){
combodepart.addItem(new DepartItem(inv.getSemesterId(),inv.getSDepartment()));
// combodepart.addActionListener(combocourse);
/*
@Override
public void actionPerformed(ActionEvent e) {
JComboBox combocourse;
combocourse = (JComboBox) e.getSource();
// Object selected = combocourse.getSelectedItem();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query= session.createQuery("FROM Senior.entity.Semester f ");
List <Semester>results= query.list();
if (results.size() > 0){
Iterator iterate= results.iterator();
final Semester resultAccount= (Semester)iterate.next();
combodepart.removeAllItems();
for(Semester inv:results){
combodepart.addItem(new DepartItem(inv.getSemesterId(),inv.getSDepartment()));
}
});
*/
}
}
session.close();
}
請註明您的問題更明顯,並使用更好的語法。這很難理解。 – ManyQuestions
當我選擇一個部門時,我該如何稱呼課程名稱?我想我應該使用addActionlistener和actionperformed,但我不知道如何使用此代碼。 – user3624017