Coluna incorreta sendo classificada quando o JTable Header clicou
Eu tenho o seguinte código para umaRowSorterListener
. O objetivo disso é classificar uma coluna sem afetar outras colunas.
import javax.swing.event.RowSorterListener;
import javax.swing.event.RowSorterEvent;
import javax.swing.JTable;
import javax.swing.RowSorter.SortKey;
import java.util.List;
import java.util.Arrays;
public class UnrelateData implements RowSorterListener {
JTable table;
int columnSorted = -1;
Object[][] dataStore;
public UnrelateData(JTable table) {
this.table = table;
}
@Override
public void sorterChanged(RowSorterEvent e)
{
if(e.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
List<SortKey> keys = e.getSource().getSortKeys();
for (SortKey key : keys) {
if (key.getColumn() == -1) {
columnSorted = -1;
break;
} else {
columnSorted = key.getColumn();
break;
}
}
dataStore = getData();
}
if(e.getType() == RowSorterEvent.Type.SORTED) {
List<SortKey> keys = e.getSource().getSortKeys();
for (SortKey key : keys) {
if (key.getColumn() == -1) {
columnSorted = -1;
break;
} else {
columnSorted = key.getColumn();
break;
}
}
for(int i = 0; i < table.getColumnCount(); i++) {
if(i != columnSorted && columnSorted != -1) {
for (int j = 0; j < table.getRowCount(); j++) {
table.setValueAt(dataStore[i][j], j, i);
}
}
}
}
}
private Object[][] getData() {
int columnCount = table.getColumnCount();
int rowCount = table.getRowCount();
Object[][] tempData = new Object[columnCount][rowCount];
for(int i = 0; i < columnCount; i++) {
for(int j = 0; j < rowCount; j++) {
tempData[i][j] = table.getValueAt(j, i);
}
}
return tempData;
};
}
Isso funciona bem. No entanto, há uma grande falha. Se uma coluna for movida e eu tentar classificar uma coluna, ela não será classificada corretamente. Em vez disso, classifica incorretamente a coluna no local original da coluna movida.
Considerando que deve ter a aparência (Onde "Coluna 1" e "Coluna 2" permanecem sem classificação)
Alguém seria capaz de explicar por que isso ocorre e como corrigi-lo?
Nota: eu não quero usarJTableHeader.reorderingAllowed(false)
Editar
Eu adicionei o seguinte para loops no meu código e tentei variações diferentes, mas não pareceu funcionar
Tentativa 1
if(e.getType() == RowSorterEvent.Type.SORTED) {
int[] actualColumn = new int[table.getColumnCount()];
for(int i = 0; i<table.getColumnCount(); i++){
actualColumn[i] = table.convertColumnIndexToModel(i);
}
int[] actualRow = new int[tabl,e.getRowCount()];
for(int i = 0; i<table.getRowCount(); i++){
actualRow[i] = table.convertRowIndexToModel(i);
}
List<SortKey> keys = e.getSource().getSortKeys();
for (SortKey key : keys) {
if (key.getColumn() == -1) {
columnSorted = -1;
break;
} else {
columnSorted = key.getColumn();
break;
}
}
for(int i = 0; i < table.getColumnCount(); i++) {
if(i != columnSorted && columnSorted != -1) {
for (int j = 0; j < table.getRowCount(); j++) {
table.setValueAt(dataStore[i][j], actualRow[j], actualColumn[i]);
}
}
}
}
Tentativa 2
private Object[][] getData() {
int columnCount = table.getColumnCount();
int rowCount = table.getRowCount();
int[] actualColumn = new int[columnCount];
for(int i = 0; i<table.getColumnCount(); i++){
actualColumn[i] = table.convertColumnIndexToModel(i);
}
int[] actualRow = new int[rowCount];
for(int i = 0; i<table.getRowCount(); i++){
actualRow[i] = table.convertRowIndexToModel(i);
}
Object[][] tempData = new Object[columnCount][rowCount];
for(int i = 0; i < columnCount; i++) {
for(int j = 0; j < rowCount; j++) {
tempData[i][j] = table.getValueAt(actualRow[j], actualColumn[i]);
}
}
return tempData;
};
A tentativa 3 foi ao mesmo tempo uma tentativa e duas