views:

56

answers:

2

Well guys, here i am. In three days i couldn't resolve this problem.

(I'm italian, sorry for my english).

Shortly. I have a panel on which there is a JTable that show a mp3 list. Then another panel whith a JComboBox (with it i can choose the type of filter), a JTextField (where i write what i want to search/filter), and a JButton that confirm and launch the filtering operation.

The problem is that when i filter the table the first time (and the filtering work) then, if i change the type of filter with the JComboBox, filter seems to freeze on the first filter i have applied.

Example: I have this JTable that has column: "#", "Title", "Artist", "Album", "Track Number (on Album)", "Genre", "Year" and "Path" (below the code, i have tranlated the name of column, the code is italian, as me :) ). I set, with the JComboBox, the type of filter, for example: "Album". I type in the JTextField what i want and click on the JButton "Search/Filter" (That is Cerca/Filtra). The filtering/searching operation goes well...BUT.. now if i change the filter and choose for example "Year", the filtering operation is still setting on "Album". So the problem is that the filtering operation still set on the first type of filtering i choosen.

i don't understand that is a problem of setting the filter or other. The code i'll post down here has some other minor error such variables inizialized but don't used, i know it. After three days i made a big number of changes and i have no time to edit every tiny "warnings".

JComboBox listener:

public class AscoltatoreComboRicerca implements ActionListener{

private JLabel jl2;
private JComboBox jcb;
private JTextField jtf;
private TableRowSorter<MioModelloTabella> sorter;
private JButton jb;
private JTable jt;
private MioModelloTabella mmt;






public AscoltatoreComboRicerca(JTextField jtf, TableRowSorter<MioModelloTabella> sorter, JLabel jl2, JComboBox jcb, JButton jb, JTable jt, MioModelloTabella mmt){
    this.jl2 = jl2;
    this.jcb = jcb;
    this.jtf = jtf;
    this.sorter = sorter;
    this.jb = jb;
    this.jt = jt;
    this.mmt = mmt;



}

public void actionPerformed(ActionEvent e) {

    //jt.getSelectionModel().clearSelection();
    //jt.clearSelection();
    jb.setEnabled(true);
    jcb = (JComboBox)e.getSource();
    String tipo_ricerca = (String)jcb.getSelectedItem();
    System.out.println("!!!!!!!!DENTRO ALL'ASCOLTATORECOMBORICERCA, PER LA JCOMBOBOX, IL TIPO_RICERCA è: " + tipo_ricerca);
    if (tipo_ricerca == "") {
        jl2.setText("Scegli tipo di ricerca");
        jtf.setEditable(false);



    }
    else {
        jl2.setText("Inserisci " + tipo_ricerca + " : ");
        jtf.setEditable(true);

    }

    if (tipo_ricerca == "Artista"){


        //Setto l'ascoltatore dedicato per il bottone.

        jb.addActionListener(new AscoltatoreBottoni(2, jtf, jt, mmt, sorter ));

        /*AscoltatoreBottoni ab = new AscoltatoreBottoni(2, jtf, jt, mmt, sorter );
        jb.addActionListener(ab);*/

        System.out.println("!!!!!!!!DENTRO ALL'ASCOLTATORECOMBORICERCA: SELEZIONATO: ARTISTA");



    }

    if (tipo_ricerca == "Album"){

        jb.addActionListener(new AscoltatoreBottoni(3, jtf, jt, mmt, sorter ));

        /*AscoltatoreBottoni ab = new AscoltatoreBottoni(3, jtf,jt, mmt, sorter);
         jb.addActionListener(ab);*/

        System.out.println("!!!!!!!!DENTRO ALL'ASCOLTATORECOMBORICERCA: SELEZIONATO: ALBUM");


    }

    if (tipo_ricerca == "Genere"){

        jb.addActionListener(new AscoltatoreBottoni(5, jtf, jt, mmt, sorter ));

        /*AscoltatoreBottoni ab = new AscoltatoreBottoni(5, jtf, jt, mmt, sorter);
        jb.addActionListener(ab); */
        System.out.println("!!!!!!!!DENTRO ALL'ASCOLTATORECOMBORICERCA: SELEZIONATO: GENERE");


    }

    if (tipo_ricerca == "Anno"){

        jb.addActionListener(new AscoltatoreBottoni(6, jtf, jt, mmt, sorter ));

        /*AscoltatoreBottoni ab = new AscoltatoreBottoni(6, jtf, jt, mmt, sorter );
        jb.addActionListener(ab); */
        System.out.println("!!!!!!!!DENTRO ALL'ASCOLTATORECOMBORICERCA: SELEZIONATO: ANNO");

    }

}

}

JButton Listener: (Only the important parts of code)

public class AscoltatoreBottoni implements ActionListener{

private ArrayList<Mp3> lista_mp3;
private MioModelloTabella mmt, mmt2;
int col;
private JTextField jtf;
private JTable jt;
private TableRowSorter<TableModel> sorter;

[....not important constructors...]



public AscoltatoreBottoni(int col, JTextField jtf, JTable jt, MioModelloTabella mmt, TableRowSorter<TableModel> sorter){
    this.col = col;
    this.jtf = jtf;
    this.mmt = mmt;
    this.jt = jt;
    this.sorter = sorter;

}
@Override
public void actionPerformed(ActionEvent e) {

    [.....Eventi inutili da farvi vedere perché si riferiscono ad altri bottoni]

    if((e.getActionCommand()=="Cerca/Filtra") || (e.getActionCommand() == "Azzera ricerca/filtro")){

            //jt.getSelectionModel().clearSelection();
            //jt.clearSelection();
            //Per resettare la situazione.
           if (col == 0) { 
            RowFilter<Object, Object> filter = new RowFilter <Object, Object>() {

             public boolean include(Entry entry) {
             Integer tmp = (Integer) entry.getValue(col); //a seconda della scelta dell'utente qui ci va il numero della colonna.

               return tmp.intValue() >0;

             }};
             try {
             TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(mmt);

               jt.setRowSorter(sorter);

                sorter.setRowFilter(filter);
             } catch (NullPointerException a){

                 System.out.println("ERRORE IN ASCOLTATORE BOTTONI");
             }

    }

           else {

                //Prendo la stringa dal jtf.
                String ricerca = jtf.getText();
                TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(mmt);
                sorter.setRowFilter(RowFilter.regexFilter(ricerca, col));                           //Cerca le righe che rispondono al campo ricerca.
                //sorter.setSortKeys(null);
                jt.setRowSorter(sorter);



              }

    }
}

PS.: The if statements, in JComboBox listener, seems to work because the println()s are printed.

Thanks for your precious help.

+1  A: 

It looks like the problem is that you are adding a new ActionListener to the button every time the combobox value changes, but you are not removing the old listener. This means each time you change the combobox value the number of button listeners increases and they will all be notified on a click, in undefined order.

Instead of adding new listeners each time, I'd suggest that you have one listener on the button that inspects the combobox to see what kind of filtering to do. I'd suggest you refactor your code as follows:

Create a method that performs the filtering given a column and filter. You've got this code in your AscoltatoreBottoni class.

Add a listener to the button that calls this method, passing the current values of the combobox and the filter field.

Add another listener to the combobox that does the same thing.

That should do the trick.

Cameron Skinner
I'm going to try this option that seems correct. Thank you by now.
iLeW
after looking at your code, this answer seems correct +1
brian_d
Thank you very much man! I did it, i try it in every two methods you explained to me. The first time removing listener and then changing a bit of code some like you told me. The 2nd method works better. So, approved :) Thank you so much!
iLeW
+1  A: 

If you are not set on a custom coded solution, I would give http://publicobject.com/glazedlists/ a try. It is free, open source, stable and has worked like a charm for me.

brian_d
I was going to say this as well. +1
I82Much
This is great but unfotunately i'm not expected to do this :( I have to do this for university adding only library for manage mp3 tags or play mp3. Damn.
iLeW