简体   繁体   English

为什么在使用组合框actionListerner之后没有取消tableModelListener代码

[英]why is tableModelListener code is not being excquted after use of a combo box actionListerner

I have a simple program that displays tables for a database using JTable. 我有一个简单的程序,可以使用JTable显示数据库的表。 I have a combobox that list all available tables and an actionListener that sets the model to the table each time it is changed. 我有一个列出所有可用表的组合框,还有一个actionListener,它每次更改模型时都会将模型设置为表。 This works as desired. 这可以按需工作。 The problem that I am getting is when a table is selected the program never reaches the code for the tableModelListener therfore not updating the database. 我得到的问题是,当选择一个表时,程序在未更新数据库之前从未到达tableModelListener的代码。

I did some debugging by placing popup messages if the program entered certain blocks of code and the program never enters the table model listener once the combo box actionListener is activated. 如果程序输入了某些代码块并且一旦组合框actionListener被激活,则程序从不进入表模型侦听器,我通过放置弹出消息进行了一些调试。

below is the bulk of the code. 下面是大部分代码。

classes.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ev) {
            JComboBox cb = (JComboBox)ev.getSource();
            classname =(String)cb.getSelectedItem();

            String genQuery="select * from "+classname;
            String query = genQuery ;


            //adding info from databae to table/
            ResultSet rs=null;
            try {
                rs = stmt.executeQuery(query);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            table.setModel(DbUtils.resultSetToTableModel(rs));


        }               

    });

// //

//update database
    table.getModel().addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            //catch events
            JOptionPane.showMessageDialog(null,"inside table listener");
            int row = e.getFirstRow();  
            int column = e.getColumn();  
            TableModel model = (TableModel)e.getSource();  
            Object data = model.getValueAt(row, column); 
            JOptionPane.showMessageDialog(null, "row: "+ row+" column: "+column+" data "+data);

            String dt = (String)data;
            int val=(Integer) null;
            if(dt.equals("true"))
                val=1;
            if(dt.equals("false"))
                val=0;


            Object student = model.getValueAt(row, 1);
            String stud = (String)student;
            String colname =table.getColumnName(column);



            String edit="update "+classname+" set "+colname+" = ? where studentName= ?";

            try {
                conn.setAutoCommit(false);
                PreparedStatement updateinfo = conn.prepareStatement(edit);


                updateinfo.setInt(1, val);
                updateinfo.setString(2, stud);
                updateinfo.executeUpdate();
                conn.commit();
                JOptionPane.showMessageDialog(null, stud+" has been updated.");
            } catch (SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

        }



    });

Your action listener creates a new table model, and replaces the model of the JTable by this new table model. 您的动作侦听器将创建一个新的表模型,并用此新表模型替换JTable的模型。 So the TableModelListener you added before still listens to the old table model, which isn't used anymore. 因此,您之前添加的TableModelListener仍会侦听不再使用的旧表模型。

Don't replace the table model, but change its content instead. 不要替换表模型,而要更改其内容。 Or replace the table model, but add the TableModelListener to the new one. 或替换表模型,但将TableModelListener添加到新模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM