0

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
  • 133
  • 5
  • 11
  • 1
    `scrollPaneInfo.setBounds(36+20, 768 - 628+10, 400-20*2, 181-10);` Wow! talk about 'magic numbers'. Those are Voodoo! Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). More generally: .. – Andrew Thompson Aug 26 '16 at 13:09
  • 1
    .. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 26 '16 at 13:09
  • What exactly is the `dbQueries.updateAdmin()` method doing? Basically what you need to do is to write a new method which you can pass parameters with it. The parameters will be taken from the JTable cells and inserted into database. The code will be different depending on what database you're using. – Payam Aug 26 '16 at 14:18
  • @Payam Thanks for your reply. dbQueries.updateAdmin() is nothing but creating DBconnect and executing the query with received value from updated table cells. I was trying to print the changed value as following: System.out.println(adminInfoTable.getValueAt( adminInfoTable.getSelectedRow(), adminInfoTable.getEditingColumn() )); Which prints oldValue. How can I get NewValue and pass in UPDATEADMIN()? – Sadequer Rahman Aug 26 '16 at 15:12

2 Answers2

0

I think you need to construct the Model Object when loading from the Database and accomplish a method to track down the changes within the model. Something like a property called isUpdated.

Then you can have external action on button to save model which is having the isUpdated true. And then reload or just reset the isUpdated value.

Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
0

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.

Sadequer Rahman
  • 133
  • 5
  • 11