Estou aprendendo threads e estou com problema na hora que a thread passa pelo metodo que estipula o o tamanho das colunas.
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 2; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i)).
getTableCellRendererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
thread
private void atualizaTabela() {
thread = new Thread() {
@Override
@SuppressWarnings("SleepWhileHoldingLock")
public void run() {
while (true) {
if (ece.isIsValida()) {
tableModel = new MyTableModel(nomeColuna, aplicacao.getTableAutores());
consultaTabela.setModel(tableModel);
setButtonsColumns(consultaTabela, consultaTabela.getColumnModel().getColumn(2), consultaTabela.getColumnModel().getColumn(3));
initColumnSizes(consultaTabela);//seu eu tirar funciona<<<<<<<<<
ece.setIsValida(false);
}
try {
sleep(300);
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, "aqui");
Logger.getLogger(InterfaceConsultaAutor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
thread.start();
}
Esta thread simplismente atualiza os campos editados na tabela.
erro
Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:427)
@Override
//o erro aponta para este bloco do meu tableModel
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
Desde já agradeço!