I have the following code for a RowSorterListener
. The purpose of this is to sort a column without affecting any other columns.
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;
};
}
This works well. However, there is one major glitch. If a column is moved and I try to sort a column it doesn't correctly sort the column. Instead, it incorrectly sorts the column in the original place of the column moved.
Whereas it should look like (Where "Column 1" and "Column 2" remain unsorted)
Would someone be able to explain why this occurs and how to fix it?
Note: I do not want to use JTableHeader.reorderingAllowed(false)
Edit
I added the following for loops into my code and tried different variations but it didn't seem to work
Attempt 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[table.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]);
}
}
}
}
Attempt 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;
};
Attempt 3 was both attempt one and two put together
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…