如何将动态变量添加到JTextField?

时间:2015-11-03 13:58:03

标签: java swing jtable counting

this is my output generated. As you can see the JTextField isn't being modified according to the totalValue

enter image description here

我是Java的初学者,我需要这方面的帮助;我正在使用JTable做POS系统。

在将费用(列)加在一起时,我很难获得正确的总数,但我没有任何错误。

目前我的主要问题是:如何在jtextfield中添加动态变量。同样,当我在数量列中编辑一个数字时,如何在jtextfield中显示新的总数。我的jtextfield(totalField)永远不会被更新。

这是我的一段代码;

   public class EditableTable extends JPanel {

   private JTextField filterField = new JTextField();
   private JButton filterButton = new JButton("Filter");
   public JTextField totalField = new JTextField(10);


   private JScrollPane scroll;
   public JPanel panel = new JPanel(new BorderLayout());
   public JPanel filterPanel = new JPanel(new BorderLayout());  
   public JPanel receiptPanel = new JPanel(new BorderLayout());
   public JPanel transactionPanel = new JPanel();
   JPanel cashPanel = new JPanel();
   JPanel totalPanel = new JPanel();

   private TableModelListener tableModelListener;


   ProductsTableModel model = new ProductsTableModel();
   final JTable table = new JTable(model);

   Double total;
   double totalValue; 
   double payment;

   public JLabel cashLab = new JLabel("Cash");
   public JTextField cashInput = new JTextField(10);

   public JLabel totalLab = new JLabel("Total");

   public JLabel changeLab = new JLabel("");

   public JButton confirmButton = new JButton("Confirm");
   public double change;


public EditableTable(){


    table.getModel().addTableModelListener(new CheckBoxModelListener());
    // final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    final TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(table.getModel());
    table.setRowSorter(rowSorter);


    table.setPreferredScrollableViewportSize(new Dimension(500, 700));
    table.setFillsViewportHeight(true);
    table.setCellSelectionEnabled(true);
    KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);
    InputMap map = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    map.put(tab, "selectNextRowCell");


    scroll = new JScrollPane(table);

    filterPanel.add(new JLabel("Specify a word to match: "), BorderLayout.WEST);
    filterPanel.add(filterField, BorderLayout.CENTER);






    filterField.getDocument().addDocumentListener(new DocumentListener(){

        @Override
        public void insertUpdate(DocumentEvent e){
            String text = filterField.getText();

            if(text.trim().length() == 0){
                rowSorter.setRowFilter(null);
            }else{
                rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e){
            String text = filterField.getText();

            if(text.trim().length() == 0){
                rowSorter.setRowFilter(null);
            }else{
                rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e){
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });

    panel.add(scroll, BorderLayout.CENTER);
    panel.add(filterPanel, BorderLayout.SOUTH);

    cashPanel.add(cashLab);
    cashPanel.add(cashInput);
    receiptPanel.add(cashPanel, BorderLayout.CENTER); 

    cashInput.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            payment = Double.parseDouble(cashInput.getText());
            change = payment - totalValue;
            changeLab.setText("" + change);
            System.out.println("Change" + change);
        }
    });


    receiptPanel.add(new JLabel("Change " + change), BorderLayout.SOUTH);
    totalPanel.add(totalLab);
    totalPanel.add(totalField);
    receiptPanel.add(totalPanel, BorderLayout.NORTH);

   table.getModel().addTableModelListener(new TableModelListener(){
       @Override
       public void tableChanged(TableModelEvent ev){
           if(ev.getColumn() == 3|| ev.getColumn() == 4){
               calculateTotal();
            }
        }
    });


}

//Simple method to calculate the total and set it to the JTextField
public void calculateTotal(){
    double total = 0d;
    int numOfRows = table.getRowCount();
    for(int i = 0; i < numOfRows; i++){
        Object cost = table.getValueAt(i,5);
        if(cost instanceof Number){
            total += ((Number)cost).doubleValue();
        }
    }
    System.out.println("Total" + total);

    totalField.setText(NumberFormat.getNumberInstance().format(total));
    System.out.println("I DID IT");
}


}

真的非常感谢任何帮助

这是我最新的更新!我想我的 receiptPanel 可能会出现问题,其中涉及 totalField

1 个答案:

答案 0 :(得分:1)

对于很多评论,我发布了一个答案,以便您了解如何使用TableModel和侦听器。

    table = new JTable();
    //I override the getValueAt see @mKorbel comments so that columns 5
    //return the value of 3*4 (you should not to calculate and set this from   
    //the listener, setting the value will recall the listener again)
    table.setModel(new DefaultTableModel() {

        @Override
        public Object getValueAt(int row, int column) {
            if (column == 5) {
                Object price = super.getValueAt(row, 3);
                Object quantity = super.getValueAt(row, 4);
                if (price instanceof Number && quantity instanceof Number) {
                    return ((Number) price).doubleValue() * ((Number) quantity).intValue();

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

    });

    //I use the listener to understand if column 3 or 4 is changed
    //to recalculate my totale. I have also added code to detect inseration/delation of rows.
    table.getModel().addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent ev) {
            switch(ev.getType()){
            case TableModelEvent.UPDATE:
                if (ev.getColumn() == 3 || ev.getColumn() == 4) {
                    calculateTotale();
                }
                return;
            case TableModelEvent.INSERT:
            case TableModelEvent.DELETE:
                calculateTotale();
                break;
            }
        }
    });
}

//Simple metod to calculate the total and set it to the JTextField.
public void calculateTotale() {
    double tot = 0d;
    int numOfRows = table.getRowCount();
    for (int i = 0; i < numOfRows; i++) {
        Object cost = table.getValueAt(i, 5);
        if (cost instanceof Number) {
            tot += ((Number) cost).doubleValue();
        }
    }
    totalField.setText(NumberFormat.getNumberInstance().format(tot));
    System.out.println("I DID IT!!");
}

注意这不是复制和过去的代码,它是一个简短的代码示例,让您了解JTable可以工作。

如果您在控制台中看到 I DID IT !! 并且JTextField没有改变,则totalField不是您认为的总字段!...我猜可能是;您正在进行new JTextField(),将其添加到布局中,然后再次执行new JTextField()(这会更改其引用)。