Skip to main content

SwingX1_6_2_MatchingTextHighlighter

3 replies [Last post]
nenadm
Offline
Joined: 2008-11-11
Points: 0

Hello,

I am interested is there support in SwingX 1.6.2 for matching text highlighting in JXTable.
For example if i enter 'ABC' i want all cells in JXTable that contains matching text to be highlighted (with some different color), but only part 'ABC' - not whole label.

Any help will be appreciated.

Best regards.

Reply viewing options

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

I did a custom renderer to do that for a JXTreeTable. Simply replace the matching text with html to highlight it all in the renderer.

Here is part of the solution, I hope I didn't remove too much :

public class TestTreeCellRenderer extends DefaultTreeCellRenderer {

    private Pattern pattern = null;
    private String search = null;

    public TestTreeCellRenderer () {
    }

    public void setHighlightString(String search) {
        this.search = Strings.emptyToNull(search);
        this.pattern = Pattern.compile(String.format("(?i)(%s)", Pattern.quote(search)));
    }
   
    @Override
    public void updateUI() {
        setTextSelectionColor(null);
        setTextNonSelectionColor(null);
        setBackgroundSelectionColor(null);
        setBackgroundNonSelectionColor(null);
        super.updateUI();
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        JLabel renderer = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        renderer.setOpaque(false);

            // Search Highlight
            if (!selected && !Strings.isNullOrEmpty(search)) {
                if (a.getDescription().toLowerCase().contains(search.toLowerCase())) {
                    renderer.setOpaque(true);
                    renderer.setText(text2htmlHighlithed(getText(), search));
                    renderer.setForeground(getTextNonSelectionColor());
                }
            }

        return renderer;
    }

    private String text2htmlHighlithed(String text, String search) {
        String out = search;
        Matcher m = pattern.matcher(text);
        if (m.find()) {
            out = m.replaceAll("<font bgcolor=#FFFF00>$0<font>"); // replace with matched group, to preserve case of that match
        }

        return String.format("<html>%s", out);
    }
}
kleopatra
Offline
Joined: 2003-06-11
Points: 0

we have a version in the demos (never made it to core ;-), see

https://java.net/projects/swinglabs-demos/sources/svn/show/trunk/SwingXS...

should work in 1.6.2 as well, no changes over last year or so. Anyway, I would suggest your update your version to the current, which is 1.6.5-1 (available via maven.java.net)

Cheers
Jeanette

nenadm
Offline
Joined: 2008-11-11
Points: 0

Hello,

Example works as expected, but i have some problems to customize it. My idea is to highlight all matches at once without using JXFindBar (want use custom control for highlighting). I tried following without success:
SearchPredicate predicate = new SearchPredicate(".*o.*");
MatchingTextHighlighter matchingTextMarker = new XMatchingTextHighlighter();
matchingTextMarker.setPainter(DecoratorFactory.createPlainPainter());
matchingTextMarker.setHighlightPredicate(predicate);
((AbstractSearchable) table.getSearchable()).setMatchHighlighter(matchingTextMarker);
I this example i want to highlight ALL 'o' characters in ALL table cells AT ONCE. I suppose that i need to call doHighlight somehow but cannot figure out how.
Any help will be appreciated.

Best regards.