0
votes

I have a JTable and it setAutoCreateRowSorter(true) so that I can sort the row. When I add a row to the table without using sorting, it works fine. But When I sort the table first, (just clicking the column title and rows will be sorted), then add a row to the table bugged.
I used tableModel.addRow(new Object[]{row,row}); to add a row, TableModelListener to listen the insert event. The bug is in the TableModelListener, which means I couldn't find the row I just added, java.lang.ArrayIndexOutOfBoundsException: 3
Here's my SSCCE, it's a dialog with a table, clicking the button 'add' to Add a row to the table.

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import java.awt.GridLayout;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class MainTable extends JDialog {


private static final long serialVersionUID = 156332386872772726L;

private final JPanel contentPanel = new JPanel();
private DefaultTableModel tableModel;
private JTable table;

public static void main(String[] args) {
    try {
        MainTable dialog = new MainTable();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public MainTable() {
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
    {
        JScrollPane scrollPane = new JScrollPane();
        contentPanel.add(scrollPane);
        {
            table = new JTable();
            table.setAutoCreateRowSorter(true);
            tableModel = new DefaultTableModel(new String[]{"first","second"},0);
            table.setModel(tableModel);
            tableModel.addTableModelListener(new TableModelListener(){
                @Override
                public void tableChanged(TableModelEvent arg0) {
                    if(arg0.getType() == TableModelEvent.INSERT){
                        int row = arg0.getFirstRow();
                        System.out.println(table.getValueAt(row, 0));//bug!! I couldn't find the row I added.
                    }
                }
            });
            scrollPane.setViewportView(table);
        }
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("Add");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    int row = tableModel.getRowCount();
                    tableModel.addRow(new Object[]{row,row});//click button to add a row
                }
            });
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
    }
}

}

What I tried

  • convert row index to model index doesn't work.
  • use tableModel.insert() instead tableModel.add(),doesn't work.
1

1 Answers

2
votes

convert row index to model index doesn't work.

A TableModelListener is a listener for changes to the TableModel. So indexes in the TableModelEvent are relative to the TableModel.

System.out.println(table.getValueAt(row, 0));

Should be:

System.out.println(table.getModel().getValueAt(row, 0));

to get the data from the TableModel.

Or if you want to get the data from the JTable, then you need to use the convertRowIndexToView(...) method.