当我在JTable

时间:2016-08-01 10:14:32

标签: java swing jtable listener tablemodel

我有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();
                }
            }
         }
    });

它有一个错误:

  

在MainFrame $ 3.tableChanged(MainFrame.java:188)at   javax.swing.table.AbstractTableModel.fireTableChanged(未知来源)   在javax.swing.table.AbstractTableModel.fireTableCellUpdated(未知   来源)在javax.swing.table.DefaultTableModel.setValueAt(未知   来源)在javax.swing.JTable.setValueAt(未知来源)

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

My Table

3 个答案:

答案 0 :(得分:1)

您应该更改选定的列和行,而不是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);
  }

}

答案 1 :(得分:1)

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

Key

答案 2 :(得分: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()读取同一行中其他列的值。