Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - Same code to search a JTable with RowSorter and RowFilter only works in 2 out of 4 JTables

I'm working on a Java GUI project in NetBeans and have two different JPanels with JTables and a JTextField that the user can type into in order to search for items in the JTable.

It looks like this .

When the user types into the JTextField the KeyTyped event is called which calls my filterSearchResults() method. This method makes a new TableRowSorter for each of the 4 JTables I am working with every time it is called (I know it's inefficient but I am just shooting for something that works at the moment).

The search function works perfectly for the first two tables (jTable1 and jTable2) but does absolutely nothing for the second two even though it seems I'm doing the exact same process for both.

The tables are contained in jScrollPanes which are in jTabbedPanes in different tabs.

Here is what the GUI hierarchy looks like.

For jTable1 and jTable2 the table updates based on the search query entered in the text field and it looks like this

When the user types into the text field for the other 2 tables nothing happens. I was under the impression that there was no need to call an update function for the tables and I don't believe that there is an update function called for JTable1 and JTable2 at any point.

I can't explain the discrepancy and I don't know why the filtering doesn't appear on the finalMaleGroupsTable and finalFemaleGroupsTable. The filtering function is as follows:

    //UPDATE JTABLE FROM SEARCH FILTER
    void filterSearchResults(int x){
        firstRowSorter = new TableRowSorter<>(jTable1.getModel());
        secondRowSorter = new TableRowSorter<>(jTable2.getModel());
        thirdRowSorter = new TableRowSorter<>(finalMaleGroupsTable.getModel());
        fourthRowSorter = new TableRowSorter<>(finalFemaleGroupsTable.getModel());

            String text = jTextField1.getText();
            jTable1.setRowSorter(firstRowSorter);
                if (text.trim().length() == 0) {
                    firstRowSorter.setRowFilter(null);
                } else {
                    firstRowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
                
            jTable2.setRowSorter(secondRowSorter);
                if (text.trim().length() == 0) {
                    secondRowSorter.setRowFilter(null);
                } else {
                    secondRowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
                
            text = jTextField5.getText();
            finalMaleGroupsTable.setRowSorter(thirdRowSorter);
                if (text.trim().length() == 0) {
                    thirdRowSorter.setRowFilter(null);
                } else {
                    thirdRowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
                
                finalFemaleGroupsTable.setRowSorter(fourthRowSorter);
                if (text.trim().length() == 0) {
                    fourthRowSorter.setRowFilter(null);
                } else {
                    fourthRowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
        
    }

Thank you for your help.

question from:https://stackoverflow.com/questions/65903163/same-code-to-search-a-jtable-with-rowsorter-and-rowfilter-only-works-in-2-out-of

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...