Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3244122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:30:48+00:00 2026-05-17T18:30:48+00:00

Well guys, here i am. In three days i couldn’t resolve this problem. (I’m

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:30:49+00:00Added an answer on May 17, 2026 at 6:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, this is my first post here and really enjoying the site. I have
well i have this messages table with sample values like these: msg_id recipient_id read
Well this is incredibly frustrating. After being nagged by Rails that I need to
Well the subject is the question basically. Are there any version control systems out
Well, i've got a nice WPF book its called Sams Windows Presentation Foundation Unleashed.
Well behaved windows programs need to allow users to save their work when they
Well, after a long time writing .net programs in C# I started to feel
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well, what is one?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.