Skip to main content

JXTable and PatternFilter/FilterPipeline

6 replies [Last post]
bernmeister
Offline
Joined: 2004-10-10
Points: 0

Hi all,

I've built a JXTable with a few rows of text. I use a text field to capture some text which is used to filter out non-matching rows.

Given the following initial, unfiltered table data:

"abc", "def", "ghi"
"jkl", "abc", "mno"
"pqr", "mno", "stu"

and using some code like this:

PatternFilter patternFilter = new PatternFilter();
patternFilter.setPattern( "abc", 0 );
table.setFilters( new FilterPipeline( patternFilter ) );

I should only see the first two rows right?

Nope...I only see the first row. Shouldn't the pattern match work over the whole table?

I can post a the test code here if that would help.

Thanks in advance,

Bernmeister.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
SamSk
Offline
Joined: 2011-07-28
Points: 0

Thanks very much for this thread. It helped me past this very issue since I'm constrained to use Java 1.5.x and swingx JXTable. Here is the code I ended up with and it works as intended.

package com.mysite;

import org.jdesktop.swingx.decorator.PatternFilter;

//-------------------------------------
// Customized class to apply pattern filter to all columns instead of just one
//-------------------------------------
public class PatternFilterAllColumns extends PatternFilter {

    private int numColumns;

    public PatternFilterAllColumns (String searchText, int matchFlags, int numColumnsInput ) {
        super(searchText, matchFlags, 0 ); // Doesn't matter which column number is used here since the test method below loops through all columns
        numColumns = numColumnsInput;
        if( numColumns < 1 ) throw new IllegalArgumentException( "Number of columns must be >= 1." );
    }

    //-----------------------------
    // Override the test method to loop through all the columns looking for this text in each cell of this row
    //    This snippet comes from the Sun code for this class
    //    $Id: PatternFilter.java,v 1.10 2007/03/16 21:38:06 rbair
    //-----------------------------
    @Override
    public boolean test(int row) {

        // TODO: PENDING: wrong false?
        // null pattern should be treated the same as null searchString
        // which is open
        // !testable should be clarified to mean "ignore" when filtering
        if (pattern == null) {
            return false;
        }

        // ask the adapter if the column should be includes
        if (!adapter.isTestable(getColumnIndex())) {
            return false;
        }

        //-------------------
        // Loop through all columns looking for a match
        //   Stop when a match is found or the last column is tested
        //-------------------
        Object value;
        for (int whichColumn=0; whichColumn<numColumns; whichColumn++) {
            value = getInputValue(row, whichColumn);
            if (value != null) {
                if (pattern.matcher(value.toString()).find()) return true;
            }
        }

        return false;  //..no match found during loop

    }
vity01
Offline
Joined: 2006-03-25
Points: 0

I am really missing an another abstract class for filtering - a descendant of Filter which would include most of working code from PatternFilter. Only test method should be abstract and used.
I use PatternFilter for filtering dates, because I am too lazy to write another class which should be included in the library. I am aware of it's not clean at all.

Kleopatra

by default, PatternFilter can test only one column. You have to
implement handling multiple columns yourself (SwingX sorting/filtering
is no longer evolved) - I think, you can find some code in the forum
and/or incubator.

Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

bernmeister
Offline
Joined: 2004-10-10
Points: 0

Thanks...I realised what I'd done wrong and so rolled my own.

FWIW, the code is below...

private class PatternFilterAcrossAllColumns extends PatternFilter
{
private int m_numberOfColumns = -1;

public PatternFilterAcrossAllColumns( String searchText, int matchFlags, int numberOfColumns )
{
super( searchText, matchFlags, 0 ); // Default to first column for ease.

if( numberOfColumns < 0 ) throw new IllegalArgumentException( "Number of columns cannot be negative." );

m_numberOfColumns = numberOfColumns;
}

@Override
public boolean test( int row )
{
Pattern thePattern = getPattern();
if( thePattern == null ) return false;

for( int i = 0; i < m_numberOfColumns; i++ )
{
String text = getInputString( row, i );
if( text == null || text.length() == 0 )
continue;

if( thePattern.matcher( ".*" + text + ".*" ).find() )
return true;
}

return false;
}
}

szermierz
Offline
Joined: 2003-06-15
Points: 0

bernmeister, you da man!

saved myself some brain power because your code snippet works perfectly.

Thanks!

kschaefe
Offline
Joined: 2006-06-08
Points: 0

You've configured the filter to only check column 0.

Karl