监听器中的方法不起作用

时间:2013-09-18 09:11:04

标签: java swing listener method-call

我是一个有几个JMenuItem的班级。我添加了一个必须调用该类方法的监听器。没有编译错误,但该方法似乎没有调用。如何让监听器调用方法?

这里是我的代码,它会更好地解释:

public class FinestraOrario extends JFrame
{
private int semester;
private DBConnection connection = new DBConnection();
private Container c = this.getContentPane();
private JMenu subjMnu = new JMenu("Insegnamento");
private JMenu classMnu = new JMenu("Aula");
private JMenu profMnu = new JMenu("Docente");

    public FinestraOrario(int i)
    {
        semester = i;

        //Define menus
        JMenuBar bar = new JMenuBar();
        JMenu choice = new JMenu("Visualizza Orario per..");

        //Define frame features
        this.setSize(1150,650);
        this.setLocationRelativeTo(null);
        this.setJMenuBar(bar);

        //Add JMenu
        bar.add(choice);
        choice.add(classMnu);
        choice.add(subjMnu);
        choice.add(profMnu);

        //populate Menus
        populateMenu();

    }

    private void ShowChoosenTable(int semester, String condition)
        {
            //does something;   
        }
    private void populateMenu ()
    {

        //data from database which populate the JMenu as JmenuItem
        ArrayList<String[]> subjctesAndClasses= connection.getSemesterData(semester, "");

        ArrayList<String[]> teachers = connection.getSemesterData(semester, "keep theachers");


        Iterator<String[]> i=subjctesAndClasses.iterator(); 
        Iterator<String[]> j=teachers.iterator(); 

        Set<String> set1  = new HashSet<String>();
        Set<String> set2  = new HashSet<String>();
        Set<String> set3  = new HashSet<String>();


                while(i.hasNext())      //check values
                {
                    String[] value=i.next();    
                    String subject = value[1];  //value contains at subjects at index 1 and classes at index 2
                    String class=value[2];
                    set1.add(subject);          //erase duplicate
                    set2.add(class);                    
                }

        //getting value without duplicates
        for (String s: set1)
        {   
            JMenuItem item = new JMenuItem(s);
            subjMnu.add(item);
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                    {
                    showChoosenTable(semester, "");
                    }
                 }); 
        }

        for (String s: set2)
        {
            JMenuItem item = new JMenuItem(s);
            classMnu.add(item);

        }

                while(j.hasNext())          //idem for teacher
                {
                    String[] value=j.next();            
                    String teacher = value[0];
                    set3.add(teacher);
                }

        for (String s: set3)
        {profMnu.add(new JMenuItem(s));}

    }
}

0 个答案:

没有答案