使用JComboBox计算JTable中的运行总计

使用JComboBox计算JTable中的运行总计

本文介绍了使用JComboBox计算JTable中的运行总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JTable和Combo组合来计算运行总计,有时它计算正确,但是在选择随机值时我得到了错误的答案.

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);
      }

      public void removeCellEditorListener(CellEditorListener l) {
        editor.removeCellEditorListener(l);
      }

      public boolean shouldSelectCell(EventObject anEvent) {
        selectEditor((MouseEvent) anEvent);
        return editor.shouldSelectCell(anEvent);
      }

      protected void selectEditor(MouseEvent e) {
        int row;
        if (e == null) {
          row = table.getSelectionModel().getAnchorSelectionIndex();
        } else {
          row = table.rowAtPoint(e.getPoint());
        }
        editor = (TableCellEditor) editors.get(new Integer(row));
        if (editor == null) {
          editor = defaultEditor;
        }
      }
    }

推荐答案

请与我相比,我不能发表评论

Please I can't comment something, compare with

来自代码

import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import java.util.Hashtable;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.CellEditorListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;

public class EachRowTableEditorExample extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public EachRowTableEditorExample() {
        super("EachRow Editor Example");
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        DefaultTableModel dm = new DefaultTableModel() {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                int modelColumn = table.convertColumnIndexToModel(column);
                return (modelColumn == 2) ? false : true;
            }
        };
        dm.setDataVector(new Object[][]{
                    {new Double(0), new Integer(2), new Double(0.0)},
                    {new Double(0), new Integer(6), new Double(0.0)},
                    {new Double(0), new Integer(11), new Double(0.0)},
                    {new Double(0), new Integer(5), new Double(0.0)},
                    {new Double(0), new Integer(30), new Double(0.0)}},
                new Object[]{"Combos", "Amount", "Running Total"});
        dm.addTableModelListener(new MyTableModelListener());
        table = new JTable(dm);
        table.setRowHeight(20);
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new Double(0.00));
        comboBox.addItem(new Double(100.00));
        comboBox.addItem(new Double(200.00));
        comboBox.addItem(new Double(300.00));
        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem(new Double(0.00));
        comboBox1.addItem(new Double(-15.00));
        comboBox1.addItem(new Double(-25.00));
        comboBox1.addItem(new Double(35.00));
        JComboBox comboBox2 = new JComboBox();
        comboBox2.addItem(new Double(0.00));
        comboBox2.addItem(new Double(40.00));
        comboBox2.addItem(new Double(-15.00));
        comboBox2.addItem(new Double(10.00));
        JComboBox comboBox3 = new JComboBox();
        comboBox3.addItem(new Double(0.00));
        comboBox3.addItem(new Double(10.00));
        comboBox3.addItem(new Double(35.00));
        comboBox3.addItem(new Double(20.00));
        JComboBox comboBox4 = new JComboBox();
        comboBox4.addItem(new Double(0.00));
        comboBox4.addItem(new Double(-20.00));
        comboBox4.addItem(new Double(-10.00));
        comboBox4.addItem(new Double(15.00));
        EachRowEditor rowEditor = new EachRowEditor(table);
        rowEditor.setEditorAt(0, new DefaultCellEditor(comboBox));
        rowEditor.setEditorAt(1, new DefaultCellEditor(comboBox1));
        rowEditor.setEditorAt(2, new DefaultCellEditor(comboBox2));
        rowEditor.setEditorAt(3, new DefaultCellEditor(comboBox3));
        rowEditor.setEditorAt(4, new DefaultCellEditor(comboBox4));
        table.getColumn("Combos").setCellEditor(rowEditor);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll, BorderLayout.CENTER);
        setLocation(150, 100);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        EachRowTableEditorExample frame = new EachRowTableEditorExample();
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    class MyTableModelListener implements TableModelListener {

        public void tableChanged(TableModelEvent e) {
            System.out.println(e.getSource());
            if (e.getType() == TableModelEvent.UPDATE) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                if (column == 0 || column == 1) {
                    TableModel model = table.getModel();
                    int quantity = ((Integer) model.getValueAt(row, 1)).intValue();
                    double price = ((Double) model.getValueAt(row, 0)).doubleValue();
                    Double value = new Double(quantity * price);
                    model.setValueAt(value, row, 2);
                }
            }
        }
    }

    class EachRowEditor implements TableCellEditor {

        private Hashtable editors;
        private TableCellEditor editor, defaultEditor;
        private JTable table;

        /**
         * Constructs a EachRowEditor. create default editor
         *
         * @see TableCellEditor
         * @see DefaultCellEditor
         */
        public EachRowEditor(JTable table) {
            this.table = table;
            editors = new Hashtable();
            defaultEditor = new DefaultCellEditor(new JTextField());
        }

        /**
         * @param row
         *            table row
         * @param editor
         *            table cell editor
         */
        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);
        }

        public void removeCellEditorListener(CellEditorListener l) {
            editor.removeCellEditorListener(l);
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            selectEditor((MouseEvent) anEvent);
            return editor.shouldSelectCell(anEvent);
        }

        protected void selectEditor(MouseEvent e) {
            int row;
            if (e == null) {
                row = table.getSelectionModel().getAnchorSelectionIndex();
            } else {
                row = table.rowAtPoint(e.getPoint());
            }
            editor = (TableCellEditor) editors.get(new Integer(row));
            if (editor == null) {
                editor = defaultEditor;
            }
        }
    }
}

这篇关于使用JComboBox计算JTable中的运行总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 02:41