Question

I have a simple program that displays tables for a database using 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. 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.

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.

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

        }



    });
Was it helpful?

Solution

Your action listener creates a new table model, and replaces the model of the JTable by this new table model. So the TableModelListener you added before still listens to the old table model, which isn't used anymore.

Don't replace the table model, but change its content instead. Or replace the table model, but add the TableModelListener to the new one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top