-1
My Problem:
I have an Arraylist of an object that represents the number of lines in my Jtable, model class. well, all the other methods are working perfectly, the problem is the remove() I have no idea why is giving Exception when it comes to removing the last element of Arraylist. In the removal of the other element works, except the last, problem is the last.
The Exception:
Exception in thread "AWT-Eventqueue-0" java.lang.Indexoutofboundsexception: Index: 6, Size: 6
Which I don’t understand, because he informs me that index was, yes, and the right index. I tried several ways, index -1, and other changes in trying to find the reason and nothing, searched the internet and nothing too.
Code of the Jtable Model:
public class DisciplineModelTabel extends AbstractTableModel implements IEModelTable<Discipline> {
private ArrayList<Discipline> rows;
private Queue<Discipline> listRemoved;
private String[] columns = new String[]{"ID", "Disciplinas"};
private static final int COL_ID = 0;
private static final int COL_NAME = 1;
/**
* Construtor: espera um objeto do tipo Discipline para
* instanciar a lista das Disciplinas.
* @param disciplines - lista das disciplinas.
*/
public DisciplineModelTabel(ArrayList<Discipline> disciplines) {
this.rows = new ArrayList<>(disciplines);
listRemoved = new LinkedList<>();
}
/**
* Método que remove o ultimo elemento a entradar na fila.
*/
@Override
public void undoLastRemoved() {
if(!listRemoved.isEmpty()) {
Discipline discipline = listRemoved.poll();
addDiscipline(discipline);
}
}
/**
* Método adiciona o elemento retirado da Jtable
* para guardar na fila de deletados, para recuperação.
*/
@Override
public void addLastRemoved(Discipline discipline){
listRemoved.add(discipline);
}
/**
* Método que limpa a fila.
*/
@Override
public void clearlistRemoved() {
if(!listRemoved.isEmpty())
listRemoved.clear();
}
/**
* @return - True para fila cheia false para não.
*/
@Override
public boolean isEmptyList() {
return listRemoved.isEmpty();
}
/**
* @return quantidade de linhas na tabela.
*/
@Override
public int getRowCount() {
return this.rows.size();
}
/**
* @return quantidade de colunas na tabela.
*/
@Override
public int getColumnCount() {
return this.columns.length;
}
/**
* Método que retorna os valores das linhas e colunas da tabela.
* Instancia um objeto do tipo Disciplina e busca os valores de seus
* atributos
* @param rowIndex - Index da linha para retorno do valor.
* @param columnIndex - Index da coluna para retorno do valor.
* @return
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Discipline discipline = rows.get(rowIndex);
if(columnIndex == COL_ID)
return discipline.getId();
else if (columnIndex == COL_NAME)
return discipline.getName();
return "";
}
/**
* retorna o valor da linha selecionada da JTable
* retornando-o em um objeto.
* @param aValue - o item selecionado.
* @param rowIndex - index da linha do item da Jtable
* @param column - coluna do item selecionado.
*/
@Override
public void setValueAt(Object aValue, int rowIndex, int column) {
Discipline discipline = rows.get(rowIndex);
if (column == COL_ID)
discipline.setId((Integer) aValue);
else if (column == COL_NAME)
discipline.setName(aValue.toString());
}
/**
* Método que retorna o nome da coluna.
* @param columnIndex - o index da coluna
* @return - o nome da coluna.
*/
@Override
public String getColumnName(int columnIndex) {
return columns[columnIndex];
}
/**
* Método que retorna o tipo da coluna.
* @return o tipo da coluna.
*/
@Override
public Class getColumnClass(int columnIndex) {
if (columnIndex == COL_ID) {
return Integer.class;
}
return String.class;
}
/**
* Método que retonar o objeto de uma linha da JTable.
* @param rowIndex - o index da linha selecionada.
* @return O item (disciplina) da linha selecionada.
*/
@Override
public Discipline getDiscipline(int rowIndex) {
return rows.get(rowIndex);
}
/**
* Método que adiciona uma elemento a Jtable e
* atualizá-a para mostrar o elemento nela.
* @param discipline - A entidade disciplina adicionada.
*/
@Override
public void addDiscipline(Discipline discipline) {
rows.add(discipline);
int lastIndex = getRowCount() - 1;
fireTableRowsInserted(lastIndex, lastIndex);
}
/**
* Método que altera um item na Jtable e
* atualiza para mostrar a alteração na entidade.
* @param rowIndex - o index da linha selecionada para alteração
* @param marca - entidade que vai ser alterada.
*/
@Override
public void update(int rowIndex, Discipline marca) {
rows.set(rowIndex, marca);
fireTableRowsUpdated(rowIndex, rowIndex);
}
/**
* Método que remove um item da JTable e também
* atualizá-a removendo o item da JTable.
* @param rowIndex - o index da linha do item que vai ser removido.
*/
@Override
public void remove(int rowIndex) {
fireTableRowsDeleted(rowIndex, rowIndex);
listRemoved.add(rows.get(rowIndex));
rows.remove(rowIndex);
}
@Override
public String getArray() {
String disciplines = "";
for(int i = 0; i < rows.size(); i++) {
disciplines += "index " + i + " nome: " + rows.get(i).getName() + "\n";
}
return disciplines;
}
/**
* Método que habilida a edição na célula selecionada
* @param rowIndex - linha selecionada.
* @param columnIndex - coluna selecionada
* @return retorna se Verdadeiro a célula pode ser editada falso para não.
*/
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
Event calling to run the process list:
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
glassPanel.start();
queue.executeProcess();
model.clearlistRemoved();
Thread performer = new Thread(new Runnable() {
public void run() {
perform();
}
}, "Performer");
performer.start();
quit();
}
});
I am using Pattern Commands to create a process queue and run when the button OK is pressed. (queues.executeProcess).
Invoker code:
public class RemoveDiscipline implements IECommand {
ControllerDiscipline controller;
private int id;
public RemoveDiscipline(int id){
controller = new ControllerDiscipline();
this.id = id;
}
@Override
public void execute() {
controller.removeDiscipline(id);
}
}
process queues code:
public class QueuesProcess {
private Queue<IECommand> process;
public QueuesProcess() {
process = new LinkedList();
}
public void addProcess(IECommand process) {
this.process.add(process);
}
public void removeProcess(IECommand process) {
this.process.remove(process);
}
public void clearQueues() {
this.process.clear();
}
public void executeProcess() {
for(IECommand command : this.process)
command.execute();
process.clear();
}
}
The problem shoots in the line of Rows.remove(rowIndex);
The exception is because you are trying to access an invalid array Index. Where does the error pop up? If possible, add a [mcve] to simulate the problem.
– user28595
fireTableRowsDeleted(rowIndex, rowIndex);
should be called after removing the element from the list, and not before as it is in the methodremove
– user28595
yes, yes, I actually forgot to go back to the "gambiarras" I was trying to do to find the problem...actually the sequence I wrote and so: adds the list of removed; removes from the arrayList and updates Jtable.
– Marcus Vinicius
Where error shoots? Could post a [mcve] of your table and arraylist?
– user28595
Add more information I don’t know if this helps, so I created a process queue, with Pattern command, when you finish the changes, once you click OK the event calls the method of the queue class that will run all the processes in it, which calls the remove from the arrayList
– Marcus Vinicius
Well, it would be interesting for a simple jtable example to be executable.
– user28595