Results 1 to 9 of 9
Thread: How to calculate total in table?
- 07-30-2010, 12:24 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 30
- Rep Power
- 0
- 07-30-2010, 02:14 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Use a loop to get all the values in each row and add them up.
- 07-30-2010, 02:47 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 30
- Rep Power
- 0
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);
}
}
- 07-30-2010, 02:57 AM #4
Your posted code does not compile.
-
You also will want to provide more details on your problem.
- 07-30-2010, 03:23 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
I don't see a loop anywhere.
- 07-30-2010, 04:46 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 30
- Rep Power
- 0
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 );
-
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);
- 07-30-2010, 05:14 AM #9
Member
- Join Date
- Jul 2010
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
How to repaint.refresh the table (table model) with combo box selection envent
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-08-2010, 06:19 AM -
Total noob
By J_Walker in forum New To JavaReplies: 9Last Post: 04-24-2009, 03:10 AM -
Need Help for coding invoice total
By maxb in forum New To JavaReplies: 3Last Post: 11-22-2008, 04:22 PM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 05:33 PM -
Printing total out
By denisdoherty in forum New To JavaReplies: 1Last Post: 04-25-2008, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks