Skip to content

Instantly share code, notes, and snippets.

@steos
Created November 3, 2011 22:53
Show Gist options
  • Save steos/1338172 to your computer and use it in GitHub Desktop.
Save steos/1338172 to your computer and use it in GitHub Desktop.
Java/Swing/JTable quick'n'dirty filter highlighting
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultRowSorter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.sort.RowFilters;
public class TableFilterHilite {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Object[][] data = new Object[][]{
{"lorem", "ipsum", "dolor"},
{"foo", "bar", "baz"},
{"java", "scala", "clojure"},
{"python", "ruby", "php"}
};
final JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setLocationRelativeTo(null);
final JPanel content = new JPanel(new MigLayout("fill",
"[grow,:200:,fill]", "[][grow,fill]"));
f.setContentPane(content);
final JXTable tbl = new JXTable(data, new Object[] {"a", "b", "c"});
final JXSearchField search = new JXSearchField();
content.add(search, "wrap");
content.add(new JScrollPane(tbl));
search.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
String filter = search.getText().trim();
DefaultRowSorter rs = (DefaultRowSorter)tbl.getRowSorter();
rs.setRowFilter(filter.length() > 0 ?
RowFilters.regexFilter(".*?"+filter+".*?", 0, 1, 2) : null);
}
});
final Color fgSel = tbl.getSelectionForeground();
final Color bgSel = tbl.getSelectionBackground();
tbl.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
private boolean selected;
@Override public void paint(Graphics g) {
super.paint(g);
String filter = search.getText().trim();
if (filter.length() == 0) {
return;
}
String text = getText();
int index = text.indexOf(filter);
if (index == -1) {
return;
}
String preMatch = text.substring(0, index);
String match = text.substring(preMatch.length(), preMatch.length() + filter.length());
int pmw = g.getFontMetrics().stringWidth(preMatch);
int w = g.getFontMetrics().stringWidth(match);
g.setColor(selected ? fgSel : Color.yellow);
g.fillRect(pmw, 0, w, getHeight());
g.setColor(selected ? bgSel : getForeground());
Rectangle r = g.getFontMetrics().getStringBounds(match, g).getBounds();
g.drawString(match, pmw + 1, -r.y);
}
@Override
public Component getTableCellRendererComponent(JTable arg0,
Object arg1, boolean selected, boolean arg3, int arg4, int arg5) {
this.selected = selected;
return super.getTableCellRendererComponent(arg0, arg1, selected, arg3, arg4, arg5);
}
});
f.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment