TableRowSorter and convertRowIndexToModel() Problems
843805Nov 14 2006 — edited Nov 15 2006Hello everybody,
I have some problems working with a TableRowSorter for a JTable under Mustang b104.
What I'm trying to do is the following:
I have a model class that extends AbstractTableModel and implements a ListSelectionListener and holds ListSelection model.
So far so good. Now the model is registered as ListSelectionModel and TableMode to the JTable. For the ListSelectionModel I use an Adapter though to convert from the View to the Model index if the Tabel get's sorted by the TableRowSorter.
Now everything looks fine until the Table get's sorted indeed. While the selection seems to work well for some items in other cases the selection disappears or two items are selected at the same time although the SelectionModel is single selection!
The Adapter class looks like this:
public class SelectionEventConverter implements ListSelectionModel {
private ListSelectionModel listSelModel;
private JTable table;
public SelectionEventConverter(ListSelectionModel listSelModel,JTable table) {
this.listSelModel=listSelModel;
this.table=table;
}
public void addSelectionInterval(int firstIndex, int lastIndex) {
firstIndex=table.convertRowIndexToModel(firstIndex);
lastIndex=table.convertRowIndexToModel(lastIndex);
listSelModel.addSelectionInterval(firstIndex, lastIndex);
}
public void setSelectionInterval(int index0, int index1) {
index0=table.convertRowIndexToModel(index0);
index1=table.convertRowIndexToModel(index1);
this.listSelModel.setSelectionInterval(index0,index1);
}
public void removeSelectionInterval(int index0, int index1) {
index0=table.convertRowIndexToModel(index0);
index1=table.convertRowIndexToModel(index1);
this.listSelModel.setSelectionInterval(index0,index1);
}
public int getMinSelectionIndex() {
int ret=this.listSelModel.getMinSelectionIndex();
ret=table.convertRowIndexToView(ret);
return ret;
}
public int getMaxSelectionIndex() {
int ret=this.listSelModel.getMaxSelectionIndex();
ret=table.convertRowIndexToView(ret);
return ret;
}
public boolean isSelectedIndex(int index) {
index=table.convertRowIndexToModel(index);
return this.listSelModel.isSelectedIndex(index);
}
public int getAnchorSelectionIndex() {
int ret=this.listSelModel.getAnchorSelectionIndex();
ret=table.convertRowIndexToView(ret);
return ret;
}
public void setAnchorSelectionIndex(int index) {
index=table.convertRowIndexToModel(index);
this.listSelModel.setAnchorSelectionIndex(index);
}
public int getLeadSelectionIndex() {
int ret = this.listSelModel.getLeadSelectionIndex();
if(ret>=0){
ret=table.convertRowIndexToView(ret);
}
return ret;
}
public void setLeadSelectionIndex(int index) {
// if(index>-1){
index=table.convertRowIndexToModel(index);
//}
this.listSelModel.setLeadSelectionIndex(index);
}
public void clearSelection() {
this.listSelModel.clearSelection();
}
public boolean isSelectionEmpty() {
return this.listSelModel.isSelectionEmpty();
}
public void insertIndexInterval(int index, int length, boolean before) {
index=table.convertRowIndexToModel(index);
this.listSelModel.insertIndexInterval(index,
length,before);
}
public void removeIndexInterval(int index0, int index1) {
index0=table.convertRowIndexToModel(index0);
index1=table.convertRowIndexToModel(index1);
this.listSelModel.removeIndexInterval(index0, index1);
}
public void setValueIsAdjusting(boolean valueIsAdjusting) {
this.listSelModel.setValueIsAdjusting(valueIsAdjusting);
}
public boolean getValueIsAdjusting() {
return this.listSelModel.getValueIsAdjusting();
}
public void setSelectionMode(int selectionMode) {
this.listSelModel.setSelectionMode(selectionMode);
}
public int getSelectionMode() {
return this.listSelModel.getSelectionMode();
}
public void addListSelectionListener(ListSelectionListener x) {
this.listSelModel.addListSelectionListener(x);
}
public void removeListSelectionListener(ListSelectionListener x) {
this.listSelModel.removeListSelectionListener(x);
}
}
Any help would be greatly appreciated!
Ingo