Pessoal estou com um problema com uma JTable, já fiz várias vezes em outras aplicações e agora deu branco.
Estou tentanto pegar o código da primeira coluna de uma tabela, e está dando a seguinte exceção:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
at javax.swing.DefaultRowSorter.convertRowIndexToModel(Unknown Source)
at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
Integer.parseInt(tbEmpresa.getValueAt(tbEmpresa.getSelectedRow(), 0).toString());
Segue também o trecho de cógido da criação da tabela
spTabela = new JScrollPane();
jpTabela.add(spTabela);
spTabela.setBounds(10, 22, 754, 161);
TableModel tbEmpresaModel = new DefaultTableModel(
new String[][] {
{null, null, null}
},
new String[] {"C\u00F3digo", "Raz\u00E3o Social", "CNPJ"})
{
private static final long serialVersionUID = 1L;
//bloqueia a edicao da tabela
boolean[] canEdit = new boolean [] {false, false, false};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
};
tbEmpresa = new JTable();
spTabela.setViewportView(tbEmpresa);
tbEmpresa.setModel(tbEmpresaModel);
//organizacao das colunas da tabela
tbEmpresa.getColumnModel().getColumn(0).setPreferredWidth(10);
tbEmpresa.getColumnModel().getColumn(0).setCellRenderer(direita);
tbEmpresa.getColumnModel().getColumn(1).setPreferredWidth(480);
tbEmpresa.getColumnModel().getColumn(1).setCellRenderer(esquerda);
tbEmpresa.getColumnModel().getColumn(2).setPreferredWidth(80);
tbEmpresa.getColumnModel().getColumn(2).setCellRenderer(esquerda);
tbEmpresa.setAutoCreateRowSorter(true);
tbEmpresa.addMouseListener(empresaControl);
tbEmpresa.addKeyListener(empresaControl);