2

I have to search text in jtable data. e.g. "ADMIN" text is appearing multiple places in jtable then how to highlight all the cells that contains specified value.

Does anyone have any idea ?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Sanket Pipariya
  • 79
  • 1
  • 12
  • where exactly are you stuck? Basically, you have to a) loop through the data to find the cells b) have a renderer that decorates those cells – kleopatra Aug 09 '13 at 08:27
  • @kleopatra, I posted some code below but re-reading your comment I feel like something is wrong because I do both a) and b) in the cell rendered itself. What would be the correct way? – Matthieu Aug 11 '13 at 03:58
  • nothing wrong - me having been on the wrong requirement (a is needed only when navigating through search results :-) A custom renderer is just fine. – kleopatra Aug 11 '13 at 08:26

2 Answers2

2

In SwingX (biased me can't resist showing off :-) the solution boils down to installing a Highlighter and configure it with a SearchPredicate as needed:

// instantiate a background decorator
final ColorHighlighter hl = new ColorHighlighter(HighlightPredicate.NEVER, Color.YELLOW, null);
// register it with the table (of type JXTable)
table.addHighlighter(hl);
// set the predicate, f.i. highlight all cell that contain ADMIN
hl.setHighlightPredicate(new SearchPredicate("ADMIN"));
kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

As @kleopatra suggested, use a custom CellRenderer (the following example is just a POC, add methods to change the search pattern, highlight color, etc.):

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class CellHighlighter {

    private static class CellHighlighterRenderer extends JLabel implements TableCellRenderer {

        public CellHighlighterRenderer() {
            setOpaque(true); // Or color won't be displayed!
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            String val = (String)value;
            Color c;
            if (val.matches(".*MIN.*")) // Add a method to configure the regexpr
                c = Color.YELLOW; // Add a method to configure color
            else
                c = UIManager.getColor("Table.background");
            setBackground(c);
            setText(val);
            return this;
        }
    }

    public static void main(String[] args) {
        String[] columnNames = {
            "Login", "Real name", "Age", "Birthday"
        };
        String[][] data = {
            {"toto", "Toto Mackwert", "73", "18/06/1940"},
            {"adm", "ADMINISTRATOR", "13", "01/01/2000"},
            {"AMINA", "Amina Farou", "3", "01/01/2010"},
        };
        JTable table = new JTable(data, columnNames);
        table.setDefaultRenderer(Object.class, new CellHighlighterRenderer());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • There might be a better way though, I'm just a beginner in Swing. – Matthieu Aug 11 '13 at 03:54
  • just for the record: didn't suggest and you don't _override JTable_ :-) Though you can (and SwingX _does_ hook its decoration mechanism in JXTable itself): if you need different types of renderers it might be handy to put the logic into table.prepareRenderer (needs renderers that are well-behaved, though) – kleopatra Aug 11 '13 at 08:30
  • I initially overrode `JTable` but then modified the code to use `setDefaultRenderer()` instead. I'll modify my answer ;-) I'm using `prepareRenderer()` sometimes, but I'm getting confused with when to use it or `getCellRenderer()`. I might be asking such a question soon ;-) – Matthieu Aug 11 '13 at 10:48
  • Very useful code and that's exactly what i want. Thanks Matthieu – Sanket Pipariya Aug 20 '13 at 18:35