如何将已编辑的 JTable 单元格的记录更新到数据库中

How to update edited JTable cell's records into Database

我有以下方法,它在 Editable 单元格中加载包含来自数据库管理 table 的记录的 JTable。

 private void renderAdminInfoTable() {
        adminInfoTable = new JTable();
        adminInfoTable.setPreferredScrollableViewportSize(new Dimension(400, 181));
        adminInfoTable.setFillsViewportHeight(true);
        adminInfoTable.setBackground(ARSColour.TRANSPARENTBLUE);
        adminInfoTable.setGridColor(new Color(128, 128, 128, 50));
        adminInfoTable.setRowHeight(32);
        adminInfoTable.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        adminInfoTable.setShowVerticalLines(false);

        DefaultTableModel model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                switch (row){
                    case 0://Name
                        return false;
                    case 1: //Email
                        if(column == 1)
                            return true;
                        else
                            return false;
                    case 2: //Phone
                        if(column == 1)
                            return true;
                        else
                            return false;
                    default:
                        return false;
                }
            }
        };
        adminInfoTable.setModel(model);

        //Create the scroll pane and add the table to it.
        scrollPaneInfo = new JScrollPane(adminInfoTable);
        this.add(scrollPaneInfo);
        scrollPaneInfo.setBounds(36+20, 768 - 628+10, 400-20*2, 181-10);
        scrollPaneInfo.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        scrollPaneInfo.setBackground(ARSColour.TRANSPARENTBLUE);

        JScrollBar sb = scrollPaneInfo.getVerticalScrollBar();
        sb.setUI(new MyScrollbarUI());
        // sets width of scrollbar
        Dimension scrollBarDim = new Dimension(10, sb
                .getPreferredSize().height);
        sb.setPreferredSize(scrollBarDim);

    }

在另一种方法中,我有一个带有 addActionListener 的保存按钮,将调用 dbQueries.updateAdmin() 方法。

public void renderAdminInformationPanel() {
        AdminInfoLabel = new JLabel("Edit Admin Information");
        AdminInfoLabel.setFont(new Font("Open Sans", Font.PLAIN, 22));
        AdminInfoLabel.setBounds(36, 768 - 662 - 6, AdminInfoLabel.getPreferredSize().width, AdminInfoLabel.getPreferredSize().height);
        AdminInfoLabel.setForeground(ARSColour.BRIGHTBLUE);
        AdminInfoLabel.setBackground(ARSColour.WHITE);
        AdminInfoLabel.setOpaque(true);
        this.add(AdminInfoLabel);

        btnSave = new JButton("Save");
        btnSave.setBounds(196, 998 - 662 - 6, btnSave.getPreferredSize().width, btnSave.getPreferredSize().height);
        btnSave.setOpaque(true);
        this.add(btnSave);
        btnSave.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dbQueries.updateAdmin(); //HERE IS THE METHOD CALL WHERE I NEEDED TO PASS ALL EDITED COLUMNS IN ORDER TO SAVE INTO DATABASE.
            }
        });

        renderAdminInfoTable();

        tableBackground = new JPanel();
        this.add(tableBackground);
        tableBackground.setBounds(36, 768 - 628, 400, 181);
        tableBackground.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        tableBackground.setBackground(ARSColour.TRANSPARENTBLUE);

        adminInfoPanel = backgroundJPanel(24, 768-675, 424, 340);
    }

我的问题是:如何在 UPDATEADMIN() METHOD 中传递所有已编辑单元格的值以更新数据库? - 谢谢

我认为您需要在从数据库加载时构造模型对象,并完成一种方法来跟踪模型中的更改。类似 属性 的东西叫做 isUpdated。

然后您可以对按钮进行外部操作以保存 isUpdated 为真的模型。然后重新加载或重置 isUpdated 值。

谢谢大家。我找到了答案。 在进入 UPDATEADMIN() 之前,我必须停止 CellEditing()。所以这是代码:

     if(adminInfoTable.isEditing()){
                        adminInfoTable.getCellEditor().stopCellEditing();
                    }
adminInfoTable.getValueAt(

                            adminInfoTable.getSelectedRow(),
                            adminInfoTable.getSelectedColumn()
                    )

就是这样。然后我能够传递单元格编辑值。