用于更新JTable中给定单元格/列并增加焦点的侦听器类型

时间:2011-08-25 17:58:57

标签: java swing jtable listeners

我正在尝试使用预定义第一列的JTable。用户仅将数据输入第二列(数量)。然后我通过乘以服务和数量列计算最终收入,并将其显示在第三列,收入。

|Service | Quantity | Income
|$40.00  | X        | 
|$40.00  | 3        | 120 

这里用户输入“3”,因为她现在以每个40美元的价格购买了服务X的“3”。用户只能更新数量列。收入列将由系统计算。

我应该使用哪种类型的听众?我正在使用TableModelListener但是当我想通过调用setValue = $120将收入更新为120时,它会触发TableListenerEvent,因此会产生无限循环。

我应该使用ActionEventColumnListener还是别的什么?

另外,我希望“焦点”增加行,总是停留在第二列(用户编辑的列)。

1 个答案:

答案 0 :(得分:4)

对于Listening更改为Table Cell,您必须实现TableModelListener,例如

enter image description here

import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableProcessing extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;
    private String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
    private Object[][] data = {
        {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
        {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
        {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
        {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}};
    private TableModelListener tableModelListener;

    public TableProcessing() {
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override// Returning the Class of each column will allow different renderers
            public Class getColumnClass(int column) { // to be used based on Class
                return getValueAt(0, column).getClass();
            }

            @Override //  The Cost is not editable
            public boolean isCellEditable(int row, int column) {
                int modelColumn = convertColumnIndexToModel(column);
                return (modelColumn == 3) ? false : true;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        //http://stackoverflow.com/questions/7188179/jtable-focus-query/7193023#7193023
        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");
        //http://stackoverflow.com/questions/7188179/jtable-focus-query/7193023#7193023
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
        setTableModelListener();
    }

    private void setTableModelListener() {
        tableModelListener = new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getType() == TableModelEvent.UPDATE) {
                    System.out.println("Cell " + e.getFirstRow() + ", "
                            + e.getColumn() + " changed. The new value: "
                            + table.getModel().getValueAt(e.getFirstRow(),
                            e.getColumn()));
                    int row = e.getFirstRow();
                    int column = e.getColumn();
                    if (column == 1 || column == 2) {
                        TableModel model = table.getModel();
                        int quantity = ((Integer) model.getValueAt(row, 1)).intValue();
                        double price = ((Double) model.getValueAt(row, 2)).doubleValue();
                        Double value = new Double(quantity * price);
                        model.setValueAt(value, row, 3);
                    }
                }
            }
        };
        table.getModel().addTableModelListener(tableModelListener);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableProcessing frame = new TableProcessing();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}