1

I have JTable with rows and columns, I need when I edited any cell in column with index 4 -> should changes "VALUE" in the same row, but next column with index 5. I have next code, but it doesn't work

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

It has an error:

at MainFrame$3.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)

Because it has infinite cycle. Help me, please.

My Table

Community
  • 1
  • 1
GoldenScrew
  • 181
  • 2
  • 3
  • 14
  • I don't understand you problem. Can you edit and be more clear ? – davidxxx Aug 01 '16 at 10:30
  • for better help sooner post an MCVE / SSCCE, short, runnable, compilable, with hardcoded value for JTable / XxxTableModel in local variable – mKorbel Aug 01 '16 at 13:11

3 Answers3

2

For me your mistake is related to the fact that you rely on the selected column, you should rather rely on what is provided by the event itself like this:

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);
            }
        }
    }
});
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
2

It looks like column 5 depends on column 4. You can override setValueAt() and update the next column in each row like this:

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

I'm not sure how you calculate tracksToDownload, but you can read the value of other columns in the same row using super.getValueAt().

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
1

You should change the selected column and row instead of 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();
                }
            }
         }
    });

Or you could try like as below. From this example, if you edit "D" column then "E" column will be updated with "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);
  }

}
Tamil
  • 1,193
  • 9
  • 24