1

I want to update a swing table's row value in the DB, clear the model, and load the new model with the new updated value. The problem is that when I click the button to update the value, the model is cleared but no data is shown in the table. What am I doing wrong?

Parts of the code not relative to the table have been removed in order to make the example easier.

public class ShowValues extends JFrame {

    //Attributes
    private DefaultTableModel model;
    private JTable table  = new JTable()
        {
            public boolean isCellEditable(int row, int column)
            {                
                    return false;               
            };
        };

    public ShowValues() {

        btnDeleteItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                int selectedRowNumber = table.getSelectedRow();
                String selectedItemId = (String) table.getModel().getValueAt(selectedRowNumber, 0);
                //code to change selected item's state to deleted in the DB
                model.setRowCount(0); //clear model
                showTable(); //load new model
                labelMsg.setText("Item deleted");   
            }   
        });
        btnDeleteItem.setBounds(200, 320, 203, 57);
        contentPane.add(btnDeleteItem);

    }


    public void showTable()
    {
        String columnNames[] = {"Item Id", "Item Name", "Item price"};

        //code to get the data form the DB

        String dataValues[][] = new String[itemNum][3];
        for(int i=0; i<itemNum; i++)
        {
            dataValues[i][0] = idItem[i];
            dataValues[i][1] = nameItem[i]; 
            dataValues[i][2] = priceItem[i]; 
        }

        model = new DefaultTableModel(dataValues, columnNames);
        table.setModel(model);
      scrollPane = new JScrollPane(table);
    scrollPane.setBounds(17, 20, 490, 205);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    topPanel.add(scrollPane);
    }

}

1 Answer 1

1

//code to change selected item's state to deleted in the DB

Why would you want to reload all the data just because the change the value of one property in the row? That is not very efficient.

Just use the setValueAt(...) method to change the value in the JTable.

The problem is that when I click the button to update the value, the model is cleared but no data is shown in the table

The problem is you create a new JTable, but you never add the table to the viewport of your JScrollPane.

The solution is to NOT create a new JTable. Just create a new TableModel. Then you can just use:

table.setModel( theNewlyCreateTableModel );
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for answering. I add the table to the viewport of the JScrollPane but I havent posted that code just to make the eample easier and focus on the especific task of the reloading of the model.
I add the table to the viewport of the JScrollPane - no you don't!!! The code in the ActionListener invokes the showTable() method, which creates a new JTable. That table is never added to the viewport. The viewport still contains the original JTable with no data.
OK. I have updated to last lines of the showTable method where I add the table to the viewport of the JScrollPane and just create a JTable. Still same problem.
Did you repaint the panel? Did you verify the model actually contains data? The posted code looks reasonable but without the context of the entire code we can't tell what is wrong. Parts of the code not relative to the table have been removed in order to make the example easier. - posting random lines of code does not make it easier. Post a proper SSCCE that demonstrates the problem. We don't have access to your database so you need to "hard code" the data to simulate what you are doing. And don't use a null layout!
Yes, it contains data. If I write System.out.println("model "+model.getRowCount()); it says the row numbers that are in the model. What do you mean by repainting the panel? maybe that is the error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.