Search code examples
javaswingjtable

Keep initial data as it is and get sum of JTable column cell values


I added some data to JTable (dataTable), using button click event need to get sum of last column specific values to another JTable (summeryTable). I need to keep dataTable data as it is and get sum to summeryTable .But original data changing during summing process. What is the best way to get sum by keeping original data as it is?

Class MyApplication

`public class MyApplication {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("My Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DataPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}`

Class DataPanel

public class DataPanel extends JPanel {
    private JButton sumButton = new JButton();
    private JTable dataTable = new JTable();
    private JTable summeryTable = new JTable();

    public DataPanel() {
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(new GridBagLayout());
        sumButton.setText("Get Sum");
        dataTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {"Column", null, null, null},
                    {null,"01","T20","500"},
                    {null, "01", "R6", "250"},
                    {null, "02", "R6", "50"},
                    {"Slab", null, null, null},
                    {null, "03", "T10", "300"},
                    {null, "04", "T10", "150"}
                },
                new String[]{
                    "Member", "Bar mark", "Type & Size", "Weight"
                }
        ));
        summeryTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "Type & Size", " Total Weight"
                }
        ));
        JScrollPane dTableContainer = new JScrollPane(dataTable);
        JScrollPane sTableContainer = new JScrollPane(summeryTable);
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        add(dTableContainer, gbc);
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        add(sTableContainer, gbc);
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        add(sumButton, gbc);
        sumButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DefaultTableModel dataModel = (DefaultTableModel) dataTable.getModel();
                DefaultTableModel summeryModel = (DefaultTableModel) summeryTable.getModel();
                BigDecimal tWeightRsix = BigDecimal.ZERO;
                int rowCount = summeryModel.getRowCount();
                for (int j = rowCount - 1; j >= 0; j--) {
                    summeryModel.removeRow(j);
                }
                for (int i = 0; i < dataModel.getRowCount(); i++) {
                    if (dataModel.getValueAt(i, 2) == null) {
                        dataModel.removeRow(i);
                    }
                    String mark = (String) dataModel.getValueAt(i, 2);
                    if (mark.equals("R6")) {
                        BigDecimal weights = new BigDecimal((String) dataModel.getValueAt(i, 3).toString());
                        tWeightRsix = tWeightRsix.add(weights).setScale(2, RoundingMode.HALF_UP);
                    }
                }
                Object data1 = "R6";
                Object data2 = tWeightRsix;
                summeryModel.addRow(new Object[]{data1, data2});
            }
        });
    }
}

Solution

  • After doing few changes I got the expected result.

     for (int i = 0; i < dataModel.getRowCount(); i++) {
                    if (dataModel.getValueAt(i, 2) != null) {
                        String mark = (String) dataModel.getValueAt(i, 2);
                        if (mark.equals("R6")) {
                            BigDecimal weights = new BigDecimal((String) dataModel.getValueAt(i, 3).toString());
                            tWeightRsix = tWeightRsix.add(weights).setScale(2, RoundingMode.HALF_UP);
                        }
                    }
                }