如何在java中的组合框之间使用过滤器? [英] How can I use filter between comboboxes in java?

查看:73
本文介绍了如何在java中的组合框之间使用过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中有两个表作为学期表和课程表。在学期表中有semesterId,courseId,courseName和Sdepartment(部门名称)。课表具有courseId和courseName。
我有两个组合框我的jframe.First一个是选择一个部门。第二个是选择课程。我想选择课程选定的部门。
当我选择一个部门时,我如何在组合框中调用课程名称?
我的代码;

I have two tables in my database as semester table and course table.There are semesterId,courseId,courseName and Sdepartment(department name)in semester table.Course table has courseId and courseName. I have two comboboxes my jframe.First one is for select a department.Second one is select course.I want to select course as to selected department. How can i call course name in combobox when i select a department? Here my code;

      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();
     }


推荐答案

类似于:

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();
            }
        });
    }
}




如何转换示例到数据库?

How to convert this example to database?

在ActionListener中,查询数据库以获取所选部门的课程,然后创建模型。

In the ActionListener you query the database to get the courses for the selected department and then you create the model.

这篇关于如何在java中的组合框之间使用过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆