简体   繁体   中英

TableModelListener not always working

i am developing an application using java swing ,in witch there is a jTable that i imported data from a database .i want to update directly my database from the jTable .So based on a code that i found (sorry i don't remember the link ) i managed to do that using TableModelListener but i found a small problem i can use the TableModelListener only if i don't use anything else for example i have a button witch does anything if i clicked on this button that i go back to update any cell in the table nothing happens it is like the TableModelListener was deactivated .if i update the table first thing it works perfectly but if i update it after i do anything else it won't work . here is a part of my code(my code is too long) i tried to solve this problem but i couldn't

    public home() {

            initComponents();


            connection();

            jTable.getModel().addTableModelListener(new TableModelListener() {
                @Override
                public void tableChanged(TableModelEvent e) {
                    modiftable(e);
                }

            });


            if(jTable.getCellEditor()!=null)
            {
            jTable.getCellEditor().stopCellEditing();
            }


        }


 private void modiftable(TableModelEvent e) {
        if (e.getType() == TableModelEvent.UPDATE) {
            connection();
            home.model = (DefaultTableModel) ((TableModel) (e.getSource()));
            int fila = e.getFirstRow();
            int col = e.getColumn();
            String data = String.valueOf(home.model.getValueAt(jTable.getSelectedRow(), jTable.getSelectedColumn()));
            String rqt = "";
            String rqt2 = "";
            if (col == 1) {
                String nom = String.valueOf(home.model.getValueAt(jTable.getSelectedRow(), 0));
                String poste = String.valueOf(home.model.getValueAt(jTable.getSelectedRow(), 3));
                String rqqq = "SELECT `CIE/SFIN`,`SFIN OU CIE` FROM `allin` WHERE `Nom produit`='" + nom + "' and `poste`='" + poste + "'";

                String pos = "";
                String old = "";
                ResultSet rt = selection(rqqq);
                try {
                    while (rt.next()) {
                        old = rt.getString(1);
                        pos = rt.getString(2);
                        System.out.println(pos);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (pos.equals("CIE")) {
                    rqt = "UPDATE `cie` SET `CIE`='" + data + "' WHERE `CIE`='" + old + "'";
                    rqt2 = "UPDATE `produit` SET `CIE`='" + data + "' WHERE `CIE`='" + old + "'";
                    System.out.println(rqt);
                } else if (pos.equals("SFIN")) {
                    rqt = "UPDATE `sfin`  SET `SFIN`='" + data + "' WHERE `SFIN`='" + old + "'";
                    rqt2 = "UPDATE `produit` SET `SFIN`='" + data + "' WHERE `SFIN`='" + old + "'";

                    System.out.println(rqt);
                }
                DataBase db = new DataBase();
                java.sql.PreparedStatement psm;
                java.sql.PreparedStatement psm2;

                try {
                    psm = db.con.prepareStatement(rqt);
                    psm.executeUpdate(rqt);
                    psm2 = db.con.prepareStatement(rqt2);
                    psm2.executeUpdate(rqt2);

                } catch (SQLException ex) {
                    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }

    }

so i found a solution for my problem ... every time i call jTable.setModel the tableModelListener won't work so what i did is : every time i call jtable.setModel i remove the tableModelListener then i call it again like this :

            jTable.setModel(model);

            jTable.getModel().removeTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                modiftable(e);
            }

        });
            jTable.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                modiftable(e);
            }

        });


        if(jTable.getCellEditor()!=null)
        {
        jTable.getCellEditor().stopCellEditing();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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