2014-05-15 83 views
0

我的數據庫中有兩張表作爲學期表和課程表。學期表中有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(); 
    } 
+0

請註明您的問題更明顯,並使用更好的語法。這很難理解。 – ManyQuestions

+0

當我選擇一個部門時,我該如何稱呼課程名稱?我想我應該使用addActionlistener和actionperformed,但我不知道如何使用此代碼。 – user3624017

回答

0

一種方法是每次從部門組合框中選擇一個項目時重置課程組合框的模型。

喜歡的東西:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 

public class ComboBoxTwo extends JPanel implements ActionListener 
{ 
    private JComboBox<String> mainComboBox; 
    private JComboBox<String> subComboBox; 
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>(); 

    public ComboBoxTwo() 
    { 
     String[] items = { "Select Item", "Color", "Shape", "Fruit" }; 
     mainComboBox = new JComboBox<String>(items); 
     mainComboBox.addActionListener(this); 

     // prevent action events from being fired when the up/down arrow keys are used 
     mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     add(mainComboBox); 

     // Create sub combo box with multiple models 

     subComboBox = new JComboBox<String>(); 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     add(subComboBox); 

     String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; 
     subItems.put(items[1], subItems1); 

     String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; 
     subItems.put(items[2], subItems2); 

     String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; 
     subItems.put(items[3], subItems3); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     String item = (String)mainComboBox.getSelectedItem(); 
     Object o = subItems.get(item); 

     if (o == null) 
     { 
      subComboBox.setModel(new DefaultComboBoxModel()); 
     } 
     else 
     { 
      subComboBox.setModel(new DefaultComboBoxModel((String[])o)); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxTwo()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

如何這個例子轉換爲數據庫?

在ActionListener中,您查詢數據庫以獲取選定部門的課程,然後創建模型。

+0

你的回答比較好:) – ManyQuestions