How to update edited JTable cell's records into Database

Sadequer Rahman

I have following method, which is loading a JTable with records from Database Admin table in the Editable cells.

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

    }

In another method I've a Save button with addActionListener with will call dbQueries.updateAdmin() method.

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

My question is: How would I pass all edited cell's value IN UPDATEADMIN() METHOD in order to update the Database? - Thanks

Sadequer Rahman

Thank you all. I found my answer. I had to stopCellEditing() before I passed into UPDATEADMIN(). So here is the code:

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

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

That was it. Then I was able to pass cell edited value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Save edited cell/s from JTable to database

From Dev

Update mysql when cell value is edited in JTable

From Dev

How to check if a particular cell in JTable is being edited?

From Dev

how to detect enter pressed while jtable cell is being edited?

From Dev

How to mark Kendo Grid's cell as edited?

From Dev

How to update jtable cell and TableModel programmatically?

From Dev

How to update JTable data with MySQL database properly?

From Dev

How to know whether QTableWidget's cell is (not) being edited?

From Dev

NSTableView update datasource with value from edited cell?

From Dev

How to update all records of all tables in database?

From Dev

How to color cells of jtable depending on cell's value

From Dev

Android database update records

From Dev

How to check if an Excel document cell is being edited

From Dev

How to Update a JTable View

From Dev

How to merge cell in DefaultTableModel/JTable?

From Dev

How to render an image to a JTable cell

From Dev

How to append text in JTable cell

From Dev

How to make a multiline cell in Jtable

From Dev

How to change a cell in a jTable programmatically

From Dev

How to update database records within a user input range?

From Dev

How to update multiple records in database using jsp and servlet

From Dev

Update Records from ListVIEW TO Database

From Dev

How to update jtable model correctly?

From Dev

How to show database contents in jTable?

From Dev

How to get the currently selected cell's indices in JTable immediately after it is highlighted?

From Dev

How to insert, update and delete items from JTable that is loaded from (SQLite) Database

From Dev

How to manually modify a cell that is being edited in a worksheet_change event?

From Dev

How to update records in table?

From Dev

Display records from mysql database to a custom Jtable in java

Related Related

  1. 1

    Save edited cell/s from JTable to database

  2. 2

    Update mysql when cell value is edited in JTable

  3. 3

    How to check if a particular cell in JTable is being edited?

  4. 4

    how to detect enter pressed while jtable cell is being edited?

  5. 5

    How to mark Kendo Grid's cell as edited?

  6. 6

    How to update jtable cell and TableModel programmatically?

  7. 7

    How to update JTable data with MySQL database properly?

  8. 8

    How to know whether QTableWidget's cell is (not) being edited?

  9. 9

    NSTableView update datasource with value from edited cell?

  10. 10

    How to update all records of all tables in database?

  11. 11

    How to color cells of jtable depending on cell's value

  12. 12

    Android database update records

  13. 13

    How to check if an Excel document cell is being edited

  14. 14

    How to Update a JTable View

  15. 15

    How to merge cell in DefaultTableModel/JTable?

  16. 16

    How to render an image to a JTable cell

  17. 17

    How to append text in JTable cell

  18. 18

    How to make a multiline cell in Jtable

  19. 19

    How to change a cell in a jTable programmatically

  20. 20

    How to update database records within a user input range?

  21. 21

    How to update multiple records in database using jsp and servlet

  22. 22

    Update Records from ListVIEW TO Database

  23. 23

    How to update jtable model correctly?

  24. 24

    How to show database contents in jTable?

  25. 25

    How to get the currently selected cell's indices in JTable immediately after it is highlighted?

  26. 26

    How to insert, update and delete items from JTable that is loaded from (SQLite) Database

  27. 27

    How to manually modify a cell that is being edited in a worksheet_change event?

  28. 28

    How to update records in table?

  29. 29

    Display records from mysql database to a custom Jtable in java

HotTag

Archive