Java - JTable 上的过滤问题

发布于 2024-09-28 02:54:52 字数 5514 浏览 3 评论 0原文

好吧,伙计们,我在这里。三天后我无法解决这个问题。

(我是意大利人,抱歉我的英语)。

不久。我有一个面板,上面有一个显示 mp3 列表的 JTable。然后另一个面板有一个 JComboBox(用它我可以选择过滤器的类型),一个 JTextField(我在其中写下我想要搜索/过滤的内容),以及一个确认并启动过滤操作的 JButton。

问题是,当我第一次过滤表(以及过滤工作)时,如果我使用 JComboBox 更改过滤器的类型,过滤器似乎会冻结在我应用的第一个过滤器上。

示例:我有这个 JTable,其中包含以下列:“#”、“标题”、“艺术家”、“专辑”、“曲目编号(专辑上)”、“流派”、“年份”和“路径”(在代码下方) ,我已经翻译了专栏的名称,代码是意大利语,就像我一样:))。我使用 JComboBox 设置过滤器的类型,例如:“专辑”。 我在 JTextField 中输入我想要的内容,然后单击 JButton“搜索/过滤”(即 Cerca/Filtra)。过滤/搜索操作进展顺利...但是.. 现在,如果我更改过滤器并选择例如“年份”,过滤操作仍然设置在“专辑”上。 所以问题是过滤操作仍然设置在我选择的第一种过滤类型上。

我不明白这是设置过滤器或其他的问题。我将在这里发布的代码还有一些其他小错误,例如变量已初始化但未使用,我知道。三天后,我做了很多更改,但我没有时间编辑每一个微小的“警告”。

JComboBox 侦听器:

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 侦听器:(仅代码的重要部分)

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.: JComboBox 侦听器中的 if 语句似乎有效,因为 println() 已打印。

感谢您的宝贵帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-10-05 02:54:52

看起来问题在于,每次组合框值发生变化时,您都会向按钮添加一个新的 ActionListener,但不会删除旧的侦听器。这意味着每次更改组合框值时,按钮侦听器的数量都会增加,并且单击时它们都会以未定义的顺序收到通知。

我建议您在按钮上放置一个侦听器来检查组合框以查看要执行哪种过滤,而不是每次都添加新侦听器。我建议您按如下方式重构代码:

创建一个方法来执行给定列和过滤器的过滤。您的 AscoltatoreBottoni 类中已包含此代码。

将侦听器添加到调用此方法的按钮,传递组合框和过滤器字段的当前值。

将另一个侦听器添加到执行相同操作的组合框。

那应该可以解决问题。

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.

夏尔 2024-10-05 02:54:52

如果您没有设置自定义编码解决方案,我会给出 http://publicobject.com/glazedlists/尝试一下。它是免费、开源、稳定的,对我来说就像一个魅力。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文