I'm trying to calculate running totals using JTable and Combo combination, sometime it calculate right but on random value selection I get wrong answer.
Here is my code which I'm using to test running totals calculation, any help would be highly appreciated:
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;
import java.util.EventObject;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class RunningTotalCalculate extends JFrame {
public static JTable table = null;
float sumupValue = 0;
private MyComboBox cb1, cb2, cb3, cb4, cb5;
public RunningTotalCalculate() {
super("Running Total Calculate");
DefaultTableModel dm = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
if (column == 0) {
return true;
}
return false;
}
};
dm.setDataVector(new Object[][] { { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }}, new Object[] { "Combos", "Amount", "Running Total" });
table = new JTable(dm) {
public TableCellRenderer getCellRenderer(int row, int col) {
TableCellRenderer renderer = super.getCellRenderer(row, col);
int alignment = SwingConstants.RIGHT; // default alignment
return renderer;
}
};
table.setFont(new Font("Dialog", Font.BOLD, 14));
table.setAlignmentX(RIGHT_ALIGNMENT);
// Arays for cobmos values
String[] val1 = { "100.00", "200.00", "300.00" };
String[] val2 = { "-15.00", "-25.00", "35.00" };
String[] val3 = { "40.00", "-15.00", "10.00" };
String[] val4 = { "10.00", "35.00", "20.00" };
String[] val5 = { "-20.00", "-10.00", "15.00" };
EachRowRenderer rowRenderer = new EachRowRenderer();
cb1 = new MyComboBox(val1);
cb2 = new MyComboBox(val2);
cb3 = new MyComboBox(val3);
cb4 = new MyComboBox(val4);
cb5 = new MyComboBox(val5);
JCheckBox checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(JLabel.CENTER);
DefaultCellEditor comboBoxEditor1 = new DefaultCellEditor(cb1);
DefaultCellEditor comboBoxEditor2 = new DefaultCellEditor(cb2);
DefaultCellEditor comboBoxEditor3 = new DefaultCellEditor(cb3);
DefaultCellEditor comboBoxEditor4 = new DefaultCellEditor(cb4);
DefaultCellEditor comboBoxEditor5 = new DefaultCellEditor(cb5);
// ItemListener & ActionListener for combo1
cb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
fireComboEvent();
}
});
cb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String cbValue1 = (String) cb1.getSelectedItem();
if (cbValue1 != null) {
float value1 = new Float(cbValue1);
sumupValue = value1;
table.getModel().setValueAt(formatAmount(value1), 0, 1);
table.getModel().setValueAt(formatAmount(sumupValue), 0, 2);
}
}
});
// ItemListener & ActionListener for combo2
cb2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
fireComboEvent();
}
});
cb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String cbValue2 = (String) cb2.getSelectedItem();
if (cbValue2 != null) {
float value2 = new Float(cbValue2);
sumupValue = sumupValue + value2;
table.getModel().setValueAt(formatAmount(value2), 1, 1);
table.getModel().setValueAt(formatAmount(sumupValue), 1, 2);
}
}
});
// ItemListener & ActionListener for combo3
cb3.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
fireComboEvent();
}
});
cb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String cbValue3 = (String) cb3.getSelectedItem();
if (cbValue3 != null) {
float value3 = new Float(cbValue3);
sumupValue = sumupValue + value3;
table.getModel().setValueAt(formatAmount(value3), 2, 1);
table.getModel().setValueAt(formatAmount(sumupValue), 2, 2);
}
}
});
// ItemListener & ActionListener for combo4
cb4.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
fireComboEvent();
}
});
cb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String cbValue4 = (String) cb4.getSelectedItem();
if (cbValue4 != null) {
float value4 = new Float(cbValue4);
sumupValue = sumupValue + value4;
table.getModel().setValueAt(formatAmount(value4), 3, 1);
table.getModel().setValueAt(formatAmount(sumupValue), 3, 2);
}
}
});
// ItemListener & ActionListener for combo5
cb5.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
fireComboEvent();
}
});
cb5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String cbValue5 = (String) cb5.getSelectedItem();
if (cbValue5 != null) {
float value5 = new Float(cbValue5);
sumupValue = sumupValue + value5;
table.getModel().setValueAt(formatAmount(value5), 4, 1);
table.getModel().setValueAt(formatAmount(sumupValue), 4, 2);
}
}
});
EachRowEditor rowEditor = new EachRowEditor(table);
rowEditor.setEditorAt(0, comboBoxEditor1);
rowEditor.setEditorAt(1, comboBoxEditor2);
rowEditor.setEditorAt(2, comboBoxEditor3);
rowEditor.setEditorAt(3, comboBoxEditor4);
rowEditor.setEditorAt(4, comboBoxEditor5);
table.getColumn("Combos").setCellRenderer(rowRenderer);
table.getColumn("Combos").setCellEditor(rowEditor);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setSize(400, 160);
setVisible(true);
}
public static String parseAmount(String val) {
String retVal = null;
if (val == null || val.equalsIgnoreCase("")) {
val = "0";
}
try {
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.UK);
Number parse = format.parse(val);
retVal = parse.toString();
} catch (Exception e) {
retVal = "0";
}
return retVal;
}
public static String formatAmount(float val) {
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.UK);
format.setMaximumFractionDigits(0);
String value = format.format(val);
return value;
}
public class MyComboBox extends JComboBox {
MyComboBox() {
super();
}
MyComboBox(Object[] v) {
super(v);
}
MyComboBox(Vector v) {
super(v);
}
@Override
protected void fireActionEvent() {
super.fireActionEvent();
}
}
public void fireComboEvent() {
cb1.fireActionEvent();
cb2.fireActionEvent();
cb3.fireActionEvent();
cb4.fireActionEvent();
cb5.fireActionEvent();
}
public static void main(String[] args) {
RunningTotalCalculate frame = new RunningTotalCalculate();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class EachRowRenderer implements TableCellRenderer {
protected Hashtable renderers;
protected TableCellRenderer renderer, defaultRenderer;
public EachRowRenderer() {
renderers = new Hashtable();
defaultRenderer = new DefaultTableCellRenderer();
}
public void add(int row, TableCellRenderer renderer) {
renderers.put(new Integer(row), renderer);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
renderer = (TableCellRenderer) renderers.get(new Integer(row));
if (renderer == null) {
renderer = defaultRenderer;
}
return renderer.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}
class EachRowEditor implements TableCellEditor {
protected Hashtable editors;
protected TableCellEditor editor, defaultEditor;
JTable table;
public EachRowEditor(JTable table) {
this.table = table;
editors = new Hashtable();
defaultEditor = new DefaultCellEditor(new JTextField());
}
public void setEditorAt(int row, TableCellEditor editor) {
editors.put(new Integer(row), editor);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
//editor = (TableCellEditor)editors.get(new Integer(row));
//if (editor == null) {
// editor = defaultEditor;
//}
return editor.getTableCellEditorComponent(table, value, isSelected,
row, column);
}
public Object getCellEditorValue() {
return editor.getCellEditorValue();
}
public boolean stopCellEditing() {
return editor.stopCellEditing();
}
public void cancelCellEditing() {
editor.cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
selectEditor((MouseEvent) anEvent);
return editor.isCellEditable(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
editor.addCellEditorListener(l);
}