Jtable Autosorter in columns

Asked

Viewed 335 times

-1

Personal I am a few years with a difficulty that even today I could not solve completely. By adding a TableRowSorter to a JTable automatically the columns can be ordered by clicking and with that I have a problem in picking the correct index, yes I tried to use the ConvertIndexToView and ConvertIndexToModel , I have looked in several places to disable the autosorter option when clicking the columns but without success.

Here it works perfectly I selected the index 1 and the record came correct according to the selected number Aqui esta funcionando perfeitamente selecionei a index 1 e o registro veio correto conforme o numero selecionado

Second Image with the error, already in this I selected index 1 and with the filter active as shown pulls me the same record as in the previous image other than the view

inserir a descrição da imagem aqui

After talking with Articuno I discovered the error in my code I checked the oracle example that worked perfectly and checked the line of the code where I obtained the objects of the model mine was like this:

@Override
public generics getObjeto(int rowIndex) {    
   //Se a quantidade de linhas do modelo não fosse igual  da tabela
   if (tabela.getModel().getRowCount() != tabela.getRowCount()) {
        //Converta a linha para a index do modelo
        return linhas.get(tabela.convertRowIndexToModel(rowIndex));
    } else { 
         //Se não sao diferentes jogue a index para o modelo sem converter      
        return linhas.get(rowIndex);
    }
}

After analyzing the example I removed the code unnecessarily getting as follows:

@Override
    public generics getObjeto(int rowIndex) {    
        return linhas.get(tabela.convertRowIndexToModel(rowIndex));       
    }

Thanks to Articuno for the great patience, I promise to improve the posts.

  • Add a [mcve] of your code so it is possible to test the problem.

  • I will play my problem and try to show in image

  • See this: https://pt.meta.stackoverflow.com/a/5485/28595

  • Yes! I usually post the whole code but in this case it involves many classes, the model that I use in jtable is customized and also do not use the jtable itself I adapted using the Decoretor patter so I posted this way only so that it is understood my mistake. But I somehow decided to lock the column and I’m seeing what you sent me from the demo of the oracle itself I’ll see if I did something wrong during the procedure

1 answer

0


I got after a lot of research a way to disable the autosorter that happened when clicking on the columns of JTable just over-write the method isSortable of TableRowSorter to return false for all columns so by clicking on the columns the user can no longer sort the data.

TableRowSorter sorter = new TableRowSorter(tabela.getModel()){
            @Override
            public boolean isSortable(int column) {
                return false; 
            }            
        };
  • So the problem was sorting the column header and not recovering the correct Intel?

  • The problem was that when ordered it did not recover the objects that were on the List because the Dice of the list was different from the Dice of the view model. When I use to search for a Jtable based on a String I solved this problem of the List index and the View Index using convertIndextoView. But thanks for your attention.

  • I reproduced your problem and convertRowIndexToModel worked properly. The problem wasn’t that, for sure, it’s in the way you’re doing this conversion. That solves it, but only "masks" the problem. Run this example from oracle and see that the model’s input does not change: https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#Tablefilterdemo

  • You were absolutely right I’ll post the code before and the correction.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.