Skip to main content

Searchable -> SearchBar ->Own TreeCellRenderer -> How to show highlighters

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2011-03-29


[Edit] : Sorry for the html code ( I don't see it when I copy and paste the code ... don't know how to remove them )

I am new to the Highlighter pattern in SwingX, even new to the Searchable facilities...

What I want is to filter/highlight the entries of a custom JXTree.
I provide an own TreeCellRenderer like so :

<br />
private final class MyCustomTreeCellRenderer extends JXLabel<br />
                implements TableCellRenderer {</p>
<p> // ... some code omitted<br />
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {<br />
                // null value - should not occur<br />
                if ( value == null ) {<br />
                    setText( "<NA>" );<br />
                    setIcon( null );<br />
                }<br />
                // else<br />
                else {<br />
             // ... do custom rendering</p>
<p>return this;<br />
<p>}<br />

Then I tryed to write my own TreeSearchable like so :
<br />
private final class CustomTreeSearcher extends TreeSearchable {</p>
<p>        @Override<br />
        protected SearchResult findMatchAt( Pattern pattern, int row ) {</p>
<p>            String text = "";<br />
            Object userObj = /** get the use object of the node at the provided row */;</p>
<p>            if( userObj != null ) {<br />
                if( userObj instanceof MyCustomNode ) {<br />
                    text = ( ( MyCustomNode ) userObj ).getName();<br />
                    if( ( text != null ) && ( text.length() > 0 ) ) {<br />
                        Matcher matcher = pattern.matcher( text );<br />
                        if( matcher.find() ) {<br />
                            return createSearchResult( matcher, row, 0 );<br />
                        }<br />
                    }<br />
                }<br />
            }<br />
return null;<br />
}<br />

I now created a new JXFindBar :
<br />
new JXFindBar( new CustomTreeSearcher( myCustomTree ) );<br />

This works ...
whereat I don't see any highlighting ( obviously )...
If I type in some text contained in the tree the scroll pane jumps to the node the text is contained in but I want also some highlighting.

For me the complexity of the highlighing framework is a little to big ( or I am to lazy to delve through the mechanisms )...
and I am "good" in providing my own nice looking custom renderers for trees/tables and so on...
How can I achive both - using my own renderer and also the highlighting mechanism for the searchables?

Much regards,


PS: It would be nice to add some small but not too trivial demos to the javadoc's.
I started the SwingX-Demo-application lot's of times but the demo codes there are too complex for me to understand them "on the fly"...
Also I am not familiar with the beans concept and the annotations they use ( even If I can ignore them it is irritating ).

My approach was to look into the source code of the SwingX-classes to get an understanding how I got to use them, what is also not that easy, but works
like you can see above.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-12


Here is some code that might help you. I think I've posted this before, but finding things here is a mess. There is a little more going on in that code, but I think this can help you achieve what you want. It is not using a Highlighter, but the renderer of the JTable (in that case, its a JXTreeTable) :



The first part of the code show how the search field feed the Renderer with the entered search string (and will also collapse my tree nodes if they do not containt the search term) while the Renderer will highlight using HTML tags the tree 'Label'.