Google Tag Manager

Showing posts with label TableRowSorter. Show all posts
Showing posts with label TableRowSorter. Show all posts

2013/07/29

aterai

JTable highlighting and filtering with regular expression

Code

class HighlightTableCellRenderer extends JTextField implements TableCellRenderer {
  private static final Color backgroundSelectionColor = new Color(220, 240, 255);
  private static final Highlighter.HighlightPainter highlightPainter
    = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
  private String pattern = "";
  private String prev = null;

  public boolean setPattern(String str) {
    if(str==null || str.equals(pattern)) {
      return false;
    }else{
      prev = pattern;
      pattern = str;
      return true;
    }
  }
  public HighlightTableCellRenderer() {
    super();
    setOpaque(true);
    setBorder(BorderFactory.createEmptyBorder());
    setForeground(Color.BLACK);
    setBackground(Color.WHITE);
    setEditable(false);
  }
  public void clearHighlights() {
    Highlighter highlighter = getHighlighter();
    for(Highlighter.Highlight h: highlighter.getHighlights()) {
      highlighter.removeHighlight(h);
    }
  }
  @Override public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected,
    boolean hasFocus, int row, int column) {
    String txt = value!=null ? value.toString() : "";
    clearHighlights();
    setText(txt);
    setBackground(isSelected ? backgroundSelectionColor : Color.WHITE);
    if(pattern!=null && !pattern.isEmpty() && !pattern.equals(prev)) {
      Matcher matcher = Pattern.compile(pattern).matcher(txt);
      if(matcher.find()) {
        int start = matcher.start();
        int end   = matcher.end();
        try{
          getHighlighter().addHighlight(start, end, highlightPainter);
        }catch(BadLocationException e) {
          e.printStackTrace();
        }
      }
    }
    return this;
  }
}

References

2009/03/02

aterai

Reset RowFilter before sorting

Code

final RowFilter < TableModel, Integer > filter = new RowFilter () {
  @Override
  public boolean include(Entry < ? extends TableModel, ? extends Integer > entry) {
    int vidx = table.convertRowIndexToView(entry.getIdentifier());
    return vidx < USER_SPECIFIED_NUMBER_OF_ROWS;
  }
};
final TableRowSorter sorter = new TableRowSorter(model) {
  @Override
  public void toggleSortOrder(int column) {
    if(check.isSelected()) {
      //((AbstractTableModel)table.getModel()).fireTableDataChanged();
      allRowsChanged();
      super.toggleSortOrder(column);
      allRowsChanged();
    }else{
      super.toggleSortOrder(column);
    }
  }
};
//final TableRowSorter sorter = new TableRowSorter < TableModel > (model) {
//  @Override
//  public void toggleSortOrder(int column) {
//    if(check.isSelected()) {
//      RowFilter < ? super TableModel, ? super Integer > f = getRowFilter();
//      setRowFilter(null);
//      super.toggleSortOrder(column);
//      setRowFilter(f);
//    }else{
//      super.toggleSortOrder(column);
//    }
//  }
//};
table.setRowSorter(sorter);
sorter.setSortKeys(Arrays.asList(new RowSorter.SortKey(1, SortOrder.DESCENDING)));

References

2008/09/16

aterai

JTable Tri-state row sorting (cycle through ascending descending unsorted)

Code

TableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//sort toggles ascending descending unsorted (like TableSorter.java)
TableRowSorter< TableModel > sorter = new TableRowSorter< TableModel >(model) {
  @Override
  public void toggleSortOrder(int column) {
    if(column >= 0 && column < getModelWrapper().getColumnCount() && isSortable(column)) {
      List< SortKey > keys = new ArrayList< SortKey >(getSortKeys());
      if(!keys.isEmpty()) {
        SortKey sortKey = keys.get(0);
        if(sortKey.getColumn()==column && sortKey.getSortOrder()==SortOrder.DESCENDING) {
          setSortKeys(null);
          return;
        }
      }
    }
    super.toggleSortOrder(column);
  }
};
table.setRowSorter(sorter);

References