How to filter Column in Jtable with Calendar type data

Asked

Viewed 101 times

0

I’m trying to filter dates using the Tablerowfilter, but it is not working properly even selecting in the filter the exact column and for example typing 24/11/2017 the filter after typing the first bar all data is lost in the Jtable.

Table model

import br.com.blogspot.denisbenjamim.decoretor.tabelas.EntradaContainersArmazenados_DecoretorTabela;
import br.com.blogspot.denisbenjamim.pojo.TransportadoraContainer;
import java.text.DecimalFormat;
import java.util.Calendar;

public class EntradaContainersArmazenados_ModelTable extends Abstract_ModeloTabelas<TransportadoraContainer> {

    private static final long serialVersionUID = 6143338956876947248L;

    public EntradaContainersArmazenados_ModelTable(EntradaContainersArmazenados_DecoretorTabela aThis, String... colunas) {
        super(aThis, colunas);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            TransportadoraContainer objeto = this.linhas.get(rowIndex);
            objeto.setTipoDocumento(TransportadoraContainer.Tipo.ENTRADA);
            switch (columnIndex) {
                case 0:
                    return objeto.getOS();
                case 1:
                    return objeto.getContainer();
                case 2:
                    return objeto.isCobrado();
                case 3:
                    return objeto.getDataPago();
                case 4:                   
                    if(objeto.getValorPago() != null)
                      return DecimalFormat.getCurrencyInstance().format(objeto.getValorPago() );
                    return null;
                case 5:
                    return objeto.getTipoPagamento();
                case 6:
                    return objeto.getTipo();
                case 7:
                    return objeto.getDataEntrada();                
                case 8:
                    return objeto.getDataSaida();
                case 9:
                    return objeto.getReservaEntrada();
                case 10:
                    return objeto.getTransportadora();
                case 11:
                    return objeto.getCNH_ENTRADA();
                case 12:
                    return objeto.getPlcCavEntrada();
                case 13:
                    return objeto.getPlcBugEntrada();
                case 14:
                    return objeto.getDs_documento();
                 case 15:
                    return objeto.isCancelado();
                case 16:
                    return objeto.getDataCancelado();
                case 17:
                    return objeto.getUsuarioQCancelou();
                default:
                    return null;
            }

        } catch (IndexOutOfBoundsException | NullPointerException boundsException) {
            boundsException.printStackTrace();

        }
        return null;
    }

    @Override
    public void setValueAt(TransportadoraContainer aValue, int rowIndex) {
        TransportadoraContainer n1 = this.linhas.get(rowIndex);
        TransportadoraContainer n2 = aValue;

        n1.setArmazenamento(n2.getArmazenamento());
        n1.setCNH_ENTRADA(n2.getCNH_ENTRADA());
        n1.setCNH_SAIDA(n2.getCNH_SAIDA());
        n1.setCobrado(n2.isCobrado());
        n1.setContainer(n2.getContainer());
        n1.setDataEntrada(n2.getDataEntrada());
        n1.setDataPago(n2.getDataPago());
        n1.setDataSaida(n2.getDataSaida());
        n1.setDs_documento(n2.getDs_documento());
        n1.setTcls(n2.getTcls());
        n1.setNm_credor(n2.getNm_credor());
        n1.setOS(n2.getOS());
        n1.setOpv(n2.getOpv());
        n1.setExportador(n2.getExportador());
        n1.setReservaEntrada(n2.getReservaEntrada());
        n1.setReservaSaida(n2.getReservaSaida());
        n1.setTerminalRetirada(n2.getTerminalRetirada());
        n1.setTipo(n2.getTipo());
        n1.setTipoPagamento(n2.getTipoPagamento());
        n1.setTransportadora(n2.getTransportadora());
        n1.setValorPago(n2.getValorPago());
        n1.setCancelado(n2.isCancelado());
        n1.setPlcBugEntrada(n2.getPlcBugEntrada());
        n1.setPlcCavEntrada(n2.getPlcCavEntrada());
        n1.setPlcBugSaida(n2.getPlcBugSaida());
        n1.setPlcCavSaida(n2.getPlcCavSaida());
        n1.setUsuarioQCancelou(n2.getUsuarioQCancelou());
        n1.setDataCancelado(n2.getDataCancelado());

        for (int i = 0; i < getColumnCount(); i++) {
            fireTableRowsUpdated(rowIndex, i);
        }
    }

    @Override
    public Class<?> getColumnClass(int i) {
        switch (i) {
            case 3:           
            case 7:
            case 8:
            case 16:
                return Calendar.class;
            case 2:
            case 15:
                return Boolean.class;
            default:
                return Object.class;
        }

    }

}

Responsible event:

 case "pesquisa":
                try {
                    if ((view.getjTF_pesquisaRegistros().getText().isEmpty()) || (8 == evt.getKeyCode())) {
                        if (view.getjCB_naoExibirDaCasa().isSelected()) {
                            getController().naoExibirRegistrosDaCasa();
                        } else {
                            getController().getSorterRegistros().setRowFilter(null);
                        }
                    } else {
                        try {
                            int coluna = view.getJcb_prmPesquisa().getSelectedIndex();
                            if (coluna < 0) {
                                Utils.painelDeMensagens("Seleciona uma Coluna", JOptionPane.INFORMATION_MESSAGE);
                                view.getJcb_prmPesquisa().requestFocus();
                                return;
                            }
                            RowFilter<JTableUtils, Object> rf = RowFilter.regexFilter("(?i)" + view.getjTF_pesquisaRegistros().getText(), coluna);
                            getController().getSorterRegistros().setRowFilter(rf);
                        } catch (PatternSyntaxException pse) {
                            JOptionPane.showMessageDialog(null, "Não existe.", "Erro", 0);
                        }
                    }
                } catch (HeadlessException exception) {
                    controller.getLOG().error("Problema com a Pesquisa", exception);
                    Utils.painelDeMensagens("Problema com a Pesquisa", JOptionPane.ERROR_MESSAGE);
                }
                break;
  • Enter the code to make the problem easier to solve.

  • Your question was already answered in a question I asked some time ago. If you want something more specific, provide a [mcve]

  • Example provided with main code which works perfectly as long as the class is not a Calendar. I ask because there must be a way to specify to Filter how it should read the data in the case of Calendar it in string form has a lot of information

  • Wouldn’t it be like Date? If it is, as I said, you already have a great answer.

  • No I use type Even Legend, I use in render and in the editor a Jdatechooser. By the way I’ll have to keep looking Thank you Articuno and please be less harsh with me

  • 1

    There’s no one being harsh here. You came up with a question, I pointed to a great solution in Victor’s answer to an identical problem I had. Now if you didn’t even want to test the solution there, then I have no fault.

  • 1

    I saw your solution is very feasible, I had read somewhere about the Rowsorter request the toString as Voce even debugged and noticed that the class was very well elaborated, Congratulations.

  • The reopening of this question is being discussed at the goal: https://pt.meta.stackoverflow.com/questions/6616/reopening-de-question-duplicate

  • Why was it reopened?

  • @Denisbenjamim I believe it was reopened by mistake or carelessness, closed as duplicate again. Judging by your comment above, it seems to me that you agree that it is duplicate, but if not agree, feel free to comment.

  • Yes I agree despite being using Calendar and not a Date as in the case of Articuno. I adapted the version in his post to my need in Custom Render/Editor that I already used.

  • Calendar is not a suitable type for what you are doing. You need to represent a point in time, that’s why there are date classes. In the case of this component, there is a method that recovers a Date type, which is the getDate().

  • It is questionable to say that a Calendar is or is not suitable for what I am doing, I find it simpler to work on many operations and it works perfectly my need. I am familiar with the getDate() component and getTime() of the Calendar class. When I made use of the example I made the appropriate adaptations I put the functionality inside the class that makes the render and the editor. I was just curious about a detail, why implement Comparable ?

  • @Denisbenjamim According to the javadoc of the TableRowSorter: "3. If the column class Implements Comparable, use the Comparator that invokes the compareTo method." That is, in columns that implement Comparable, one Comparator which invokes the method compareTo will be used. Therefore, the implements Comparable is necessary for the TableRowSorter learn how to (re)sort data using date as sort criteria.

  • Yes it is true when clicking on the columns to order the method is called. But I am noticing a horrible performance when it comes to filtering the columns of the date type even with the custom class. I think I’m gonna open up a question about this horrible performance

  • @Denisbenjamim It is better to create a different question even.

Show 11 more comments
No answers

Browser other questions tagged

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