Results 1 to 9 of 9
  1. #1
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default How to calculate total in table?

    Hi,

    I've created a table, how can I calculate total for a particular column?

    Thanks

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Use a loop to get all the values in each row and add them up.

  3. #3
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I tried the following code. But did not get the result I want.

    Can someone tell me what I did wrong?

    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.*;

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.text.NumberFormat;

    public class MoneyNest4You extends JFrame implements TableModelListener{
    protected static final String[][] Object = null;
    private static int i=1;
    private static float f=0;
    private static float total=0;
    JTable table;

    public MoneyNest4You (){
    String[] columnTitles = {"First Name",
    "Last Name",
    "Account Type",
    "Account Number",
    "Transaction Amount"};

    Object[][] data = {
    {"First Name", "Last Name",
    "Account Type", "Account Number", "Transaction Amount"},
    {"","","","", f}};
    JFrame frame = new JFrame("MoneyNest4You");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(1010, 300);
    JMenuBar menubar = new JMenuBar();

    Object[][] data = {
    {"First Name", "Last Name",
    "Account Type", "Account Number", "Transaction Amount"},
    {"","","","", f}};me.setJMenuBar(menubar);
    JMenu file = new JMenu("File");
    menubar.add(file);
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);

    JMenu help = new JMenu("Help");
    menubar.add(help);
    JMenuItem about = new JMenuItem("About");
    help.add(about);


    JPanel leftpane = new JPanel();
    JPanel rightpane = new JPanel();
    JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpane, rightpane);
    frame.add(splitpane);

    splitpane.setOneTouchExpandable(true);
    splitpane.setDividerLocation(200);
    leftpane.setBackground(Color.white);
    rightpane.setBackground(Color.gray);
    leftpane.setLayout(new GridLayout(3,1));
    rightpane.setLayout(new GridLayout(2,1));

    JButton calButton = new JButton("Calculate");
    //calButton.setPreferredSize(new Dimension(105, 50));
    JButton predictButton = new JButton("Predict");
    //predictButton.setPreferredSize(new Dimension(105, 50));
    JButton insertRowButton = new JButton("Insert Row");
    //insertRowButton.setPreferredSize(new Dimension(105, 50));
    leftpane.add(calButton);
    leftpane.add(predictButton);
    leftpane.add(insertRowButton);

    final DefaultTableModel model = new DefaultTableModel(data, columnTitles);
    model.addTableModelListener(null);
    table = new JTable(model);
    table.setPreferredSize(new Dimension(760, 300));
    rightpane.add(table);

    JScrollPane scroll = new JScrollPane(rightpane);
    splitpane.add(scroll);



    insertRowButton.addActionListener(new ActionListener(){
    public void actionPerformed (ActionEvent e) {
    model.insertRow(1, new Object[]{"","","","", f});
    i++;
    JFrame frame2 = new JFrame("Clicked");
    frame2.setVisible(true);
    frame2.setSize(200,200);
    JLabel label = new JLabel("i = " + total);
    JPanel pane = new JPanel();
    frame2.add(pane);
    pane.add(label);
    }
    });



    class exitaction implements ActionListener{
    public void actionPerformed (ActionEvent e) {
    System.exit(0);
    }
    }

    exit.addActionListener(new exitaction());
    }

    public void tableChanged(TableModelEvent e)
    {
    if (e.getType() == TableModelEvent.UPDATE)
    {
    int row = e.getFirstRow();
    int column = e.getColumn();

    if (column == 4)
    {
    TableModel model = table.getModel();
    float price = ((Float)model.getValueAt(row, 4)).floatValue();
    total = new Float(total + price);
    }
    }
    }

    public static void main(String[] args)
    {
    MoneyNest4You mn4y = new MoneyNest4You();
    mn4y.setDefaultCloseOperation( EXIT_ON_CLOSE );
    mn4y.pack();
    mn4y.setLocationRelativeTo( null );
    mn4y.setVisible(true);
    }


    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Your posted code does not compile.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I don't see a loop anywhere.

  7. #7
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I'm using Eclipse JDK to compile it, and it runs.

    I found an example of multiplying rows together, and printing the result. So I tried to edited it to summing the columns, but it doesn't work. Please take a look at this code.

    public void editingStopped( ChangeEvent e )
    {
    // System.out.println( "Editing stopped: " + e );
    // TableCellEditor editor = (TableCellEditor) e.getSource();
    // Double newValue = (Double) editor.getCellEditorValue();

    int col = 2;


    // Here we calculate new result for column 'C' in 'row'

    Object valueA = model.getValueAt( col, 1 );
    Object valueB = model.getValueAt( col, 2 );
    double valueC;

    if ( valueA instanceof Double
    && valueB instanceof Double )
    {
    valueC = (Double) valueA + (Double) valueB;
    }
    else
    {
    valueC = Double.parseDouble( String.valueOf( valueA ) ) * Double.parseDouble( String.valueOf( valueB ) );
    }

    model.setValueAt( valueC, col, 3 );
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've got to agree with Norm since your code declares a variable called "data" twice, and also has this cryptic bit of code:
    Java Code:
        {"","","","", f}};me.setJMenuBar(menubar);

  9. #9
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    sorry all, I looked at the codes I posted first. When i indented, the codes were wrong. Anyways, I looked at row summation example, and figured it out. Thank you all for trying to help.

    Cheers :)

Similar Threads

  1. Replies: 1
    Last Post: 01-08-2010, 06:19 AM
  2. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 03:10 AM
  3. Need Help for coding invoice total
    By maxb in forum New To Java
    Replies: 3
    Last Post: 11-22-2008, 04:22 PM
  4. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM
  5. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •