当我更改 JTable 列中的单元格时如何使用 setValueAt

How to use setValueAt, when I change cell in column in JTable

我有包含行和列的 JTable, 当我编辑索引为 4 的列中的任何单元格时,我需要 -> 应该更改同一行中的“VALUE”,但索引为 5 的下一列。 我有下一个代码,但它不起作用

table.getModel().addTableModelListener(new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
            if (table.getSelectedRow()>=0) {
                try {
                    if (table.getSelectedColumn()==4){
                            
                        table.setValueAt("VALUE", 0, 0);
                    }
                } catch (ArrayIndexOutOfBoundsException ee){
                     ee.printStackTrace();
                }
            }
         }
    });

有错误:

at MainFrame.tableChanged(MainFrame.java:188) at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source) at javax.swing.table.AbstractTableModel.fireTableCellUpdated(Unknown Source) at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source) at javax.swing.JTable.setValueAt(Unknown Source)

因为它无限循环。 请帮助我。

您应该更改选定的列和行而不是 table.setValueAt("VALUE", 0, 0);

        table.getModel().addTableModelListener(new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
            if (table.getSelectedRow()>=0) {
                try {
                    if (table.getSelectedColumn()==4){

                        table.setValueAt("VALUE", table.getSelectedRow(), 5);
                    }
                } catch (ArrayIndexOutOfBoundsException ee){
                     ee.printStackTrace();
                }
            }
         }
    });

或者你可以像下面这样尝试。在此示例中,如果您编辑 "D" 列,则 "E" 列将更新为 "VALUE"。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JTableListSelectionListener {

  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table;

    String[] columnTitles = { "A", "B", "C", "D", "E" };
    Object[][] rowData = { { "11", "12", "13", "14", "" }, { "21", "22", "23", "24", "" },
        { "31", "32", "33", "34", "" }, { "41", "42", "44", "44", "" } };

    table = new JTable(rowData, columnTitles);

    table.setCellSelectionEnabled(true);
    ListSelectionModel cellSelectionModel = table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {

        if(e.getValueIsAdjusting()) {
            return;
        }

        String selectedData = null;

        int[] selectedRows = table.getSelectedRows();
        int[] selectedColumns = table.getSelectedColumns();


        int selectedCol = -1;
        int selectedRow = -1;
        for (int i = 0; i < selectedRows.length; i++) {
          for (int j = 0; j < selectedColumns.length; j++) {
            selectedData = (String) table.getValueAt(selectedRows[i], selectedColumns[j]);
            selectedCol = selectedColumns[j];
            selectedRow = selectedRows[i];
          }
        }

        if(selectedCol == 3) {
            table.setValueAt("VALUE", selectedRow, selectedCol+1);
        }

        System.out.println("Selected: " + selectedData);
      }

    });

    frame.add(new JScrollPane(table));

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}

对我来说,你的错误与你依赖所选列的事实有关,你应该更依赖事件本身提供的内容,如下所示:

table.getModel().addTableModelListener(new TableModelListener() {
    public void tableChanged(TableModelEvent e) {
        if (e.getType() == TableModelEvent.UPDATE) {
            final int row = e.getFirstRow();
            final int column = e.getColumn();
            if (column == 4) {
                table.setValueAt("VALUE", row, column + 1);
            }
        }
    }
});

第 5 列似乎依赖于第 4 列。您可以覆盖 setValueAt() 并像这样更新每一行中的下一列:

@Override
public void setValueAt(Object object, int row, int col) {
    super.setValueAt(object, row, col);
    int releases = ((Integer) object).intValue();
    int tracksToDownload = releases * …;
    if (col == 4) {
        super.setValueAt(tracksToDownload, row, col + 1);
    }
}

我不确定你如何计算 tracksToDownload,但你可以使用 super.getValueAt().

读取同一行中其他列的值