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.