Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1016898 - tableWithSearcher2 - add search bar on top of JTable, return a class

JavaX fragment (include)

sclass TableSearcher {
  JTable table;
  JTextField tfInput;
  JComponent searchPanel, panel;
  F2<S, Map, Bool> rowTester;
  L<Int> rowIndices;

  S input() { ret gtt(tfInput); }
}

static TableSearcher tableWithSearcher2(final JTable t, O... _) {
  final new TableSearcher s;
  final bool precise = true;
  s.table = t;
  s.tfInput = jtextfield();
  s.rowTester = func(S pat, Map row) -> bool { anyValueContainsIgnoreCase(row, pat) };
  
  onUpdate(s.tfInput, new Runnable {
    L lastFiltered, lastOriginal;
    
    public void run() {
      S pat = s.input();
      
      L<Map<S, O>> data = rawTableData(t);
      if (eq(lastFiltered, data))
        data = lastOriginal;
        
      //print("Searching " + n(l(data), "entry"));
      new L data2;
      new L<Int> rowIndices;
      for i over data: {
        Map<S, O> map = data.get(i);
        if (isTrue(callF(s.rowTester, pat, map))) {
          data2.add(map);
          rowIndices.add(i);
        }
      }
          
      //print("Found " + n(l(data2), "entry"));
      lastFiltered = data2;
      lastOriginal = data;
      dataToTable(t, data2);
      if (precise) lastFiltered = rawTableData(t);
      s.rowIndices = rowIndices;
    }
  });
  
  s.searchPanel = withLabel("Search:", s.tfInput);
  JComponent top = s.searchPanel;
  s.panel = boolOptPar(_, 'withMargin) ? northAndCenterWithMargin(top, t) : northAndCenter(top, t);
  ret s;
}

Author comment

Began life as a copy of #1004385

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1016898
Snippet name: tableWithSearcher2 - add search bar on top of JTable, return a class
Eternal ID of this version: #1016898/11
Text MD5: 908490a201458ffbcdd913942ba3eba8
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-10 17:36:43
Source code size: 1461 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 200 / 214
Version history: 10 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1016898 & http://1016898.tinybrain.de