How to create summing row in JTable Java

65 Views Asked by At

enter image description hereI have stack somewhere

Help me creating row calculating some of values in column no. 5

I use the model below but I failed to create a row that summing up the data in column number five, please if someone know how to, help me.

I need too when data is trigger from button the row increase in number of rows as well as the last row for the total

Thank you in advance.

i

setModel ( new DefaultTableModel () {
            private static final long serialVersionUID = 1L;
            private final String[] columnNames = { "" , "" , "" , "" , "" , "" };

            @Override
            public Class<?> getColumnClass ( int columnIndex ) {
                switch ( columnIndex ) {
                    case 3:
                    case 4:
                    case 5:
                        return Double.class;
                }
                return super.getColumnClass ( columnIndex );
            }

            @Override
            public Object getValueAt ( int row ,
                    int column ) {
                if ( column == 5 ) {
                    Double i = ( Double ) getValueAt ( row ,
                            3 );
                    Double d = ( Double ) getValueAt ( row ,
                            4 );

                    if ( i != null && d != null ) {
                        return i * d;
                    } else {

                    }
                }
                return super.getValueAt ( row ,
                        column );
            }

            @Override
            public boolean isCellEditable ( int row ,
                    int column ) {
                return column == 1 || column == 2 || column == 3 || column == 4;

            }

            @Override
            public void setValueAt ( Object aValue ,
                    int row ,
                    int column ) {
                super.setValueAt ( aValue ,
                        row ,
                        column );
                fireTableCellUpdated ( row ,
                        5 );
            }

            @Override
            public String getColumnName ( int column ) {
                switch ( column ) {
                    case 0:
                        return "S/N";
                    case 1:
                        return "Product";
                    case 2:
                        return "UoM";
                    case 3:
                        return "Quantity";
                    case 4:
                        return "Price";
                    case 5:
                        return "Total";

                }
                return super.getColumnName ( column );
            }

            @Override
            public int getColumnCount () {
                return columnNames.length;
            }

        } );

        setTable ( new JTable ( getModel () ) );
        getTable ().
                setFillsViewportHeight ( true );
        getTable ().
                addMouseListener ( new MouseAdapter () {

                    @Override
                    public void mouseClicked ( MouseEvent e ) {
                        if ( e.getClickCount () == 1 ) {
                            if ( getTable ().
                                    rowAtPoint ( e.getPoint () ) < 0 ) {
                                getModel ().
                                        addRow ( new Vector () );
                                model.setValueAt ( getLine () + 1 ,
                                        getLine () ,
                                        0 );
                                setLine ( getLine () + 1 );

                            }
                        }
                    }
                } );

        tcm = table.getColumnModel ();
        tc1 = tcm.getColumn ( 0 );
        tc2 = tcm.getColumn ( 2 );
        tc1.setMaxWidth ( 100 );
        tc2.setMaxWidth ( 100 );

        getTable ().
                setRowHeight ( getTable ().
                        getRowHeight () + 15 );
        getTable ().
                getTableHeader ().
                setFont ( new Font ( "Arial" ,
                        Font.BOLD ,
                        18 ) );
        table.setFont ( new Font ( "Arial" ,
                Font.PLAIN ,
                18 ) );
        getTable ().
                setGridColor ( new java.awt.Color ( 150 ,
                        150 ,
                        150 ) );
        table.setForeground ( new java.awt.Color ( 0 ,
                0 ,
                0 ) );
        table.setBackground ( Color.WHITE );
        table.getTableHeader ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        table.getTableHeader ().
                setForeground ( new java.awt.Color ( 255 ,
                        255 ,
                        255 ) );

        getTd ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        getTd ().
                setForeground ( Color.WHITE );
        getTd ().
                setFont ( new Font ( "Arial" ,
                        Font.PLAIN ,
                        20 ) );

        getBtn1 ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        getBtn1 ().
                setFont ( new Font ( "Arial" ,
                        Font.BOLD ,
                        20 ) );
        getBtn1 ().
                setForeground ( new java.awt.Color ( 250 ,
                        250 ,
                        250 ) );
        getBtn2 ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        getBtn2 ().
                setFont ( new Font ( "Arial" ,
                        Font.BOLD ,
                        20 ) );
        getBtn2 ().
                setForeground ( new java.awt.Color ( 250 ,
                        250 ,
                        250 ) );
        getBtn4 ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        getBtn4 ().
                setFont ( new Font ( "Arial" ,
                        Font.BOLD ,
                        20 ) );
        getBtn4 ().
                setForeground ( new java.awt.Color ( 250 ,
                        250 ,
                        250 ) );
        getBtn5 ().
                setBackground ( new java.awt.Color ( 60 ,
                        130 ,
                        160 ) );
        getBtn5 ().
                setFont ( new Font ( "Arial" ,
                        Font.BOLD ,
                        20 ) );
        getBtn5 ().
                setForeground ( new java.awt.Color ( 250 ,
                        250 ,
                        250 ) );

        getBtn4 ().
                addActionListener ( ( ActionEvent e ) -> {
                    saveTable ();
                } );
        getBtn1 ().
                addActionListener ( ( ActionEvent e ) -> {
                    loadTable ();
                } );
        getBtn2 ().
                addActionListener ( ( ActionEvent e ) -> {
                    getModel ().
                            addRow ( new Vector () );
                    double total = 0;
                    model.setValueAt ( total ,
                            model.getRowCount () - 1 ,
                            5 );
                    model.setValueAt ( getLine () + 1 ,
                            getLine () ,
                            0 );
                    setLine ( getLine () + 1 );
                } );

        getBtn5 ().
                addActionListener ( new ActionListener () {
                    @Override
                    public void actionPerformed ( ActionEvent e ) {
                        MessageFormat header = new MessageFormat ( "INVOICE" );
                        MessageFormat footer = new MessageFormat ( "-{0}-\n" + new Date () );
                        try {
                            table.print ( JTable.PrintMode.FIT_WIDTH ,
                                    header ,
                                    footer );
                        } catch ( Exception ae ) {
                            System.err.println ( "Error printing: " + ae.getMessage () );

                        }
                    }
                } );

        getJb ().
                add ( getBtn5 () );
        getJb ().
                add ( getBtn1 () );
        getJb ().
                add ( getBtn4 () );
        getJb ().
                add ( getBtn2 () );

        setScrollPane2 ( new JScrollPane ( getTable () ) );

        frame.add ( getScrollPane2 () ,
                BorderLayout.CENTER );
        frame.add ( getTd () ,
                BorderLayout.NORTH );
        frame.add ( getJb () ,
                BorderLayout.SOUTH );
        frame.setDefaultCloseOperation ( EXIT_ON_CLOSE );
        frame.pack ();
        frame.setSize ( 1300 ,
                900 );
        frame.setLocationRelativeTo ( null );
        frame.setVisible ( true );

  The image can be viewed here 
1

There are 1 best solutions below

0
Juan Ignacio Visso On

First of all, you should avoid putting a summary row as the last row of the table, because you will be altering the data model. That will avoid the need of moving the row if you add/delete rows from the model. That being said, you can easily put the totals in a JLabel or other component elsewhere. Just add a TableModelListener to catch changes in the model and recalculate the totals:

dataTable.getModel().addTableModelListener(new TableModelListener() {
    @Override
    public void tableChanged(TableModelEvent e) {
        updateSummary();
    }
});

private void updateSummary(){
    Double sum = (double) 0;
    for (int row = 0; row < dataTable.getRowCount(); row++){
        int rowModel = dataTable.convertRowIndexToModel(row);
        // Make sure the cast is possible here, or convert the Object appropiately to Double
        Double value = (Double) tblResults.getModel().getValueAt(rowModel, 5);
        sum += value;
    }
    // Format the number appropiately here if you need
    lblSummary.setText(sum.toString());
}

This method will work even if you have a RowFilter, because it works with all visible rows.