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

54
LINES

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

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (20447L/126K).

1  
sclass TableSearcher {
2  
  JTable table;
3  
  JTextField tfInput;
4  
  JComponent searchPanel, panel;
5  
  F2<S, Map, Bool> rowTester;
6  
  L<Int> rowIndices;
7  
  SingleComponentPanel scpRightOfSearchPanel = scp();
8  
  L lastFiltered, lastOriginal;
9  
10  
  S input() { ret gtt(tfInput); }
11  
  
12  
  void clearSearchField { setText(tfInput, ""); }
13  
}
14  
15  
static TableSearcher tableWithSearcher2(JTable t, O... _) {
16  
  new TableSearcher s;
17  
  bool precise = true;
18  
  s.table = t;
19  
  s.tfInput = jtextfield();
20  
  s.rowTester = func(S pat, Map row) -> bool { anyValueContainsIgnoreCase(row, pat) };
21  
  
22  
  onUpdate(s.tfInput, new Runnable {
23  
    public void run() {
24  
      S pat = s.input();
25  
      
26  
      L<Map<S, O>> data = rawTableData(t);
27  
      if (eq(s.lastFiltered, data))
28  
        data = s.lastOriginal;
29  
        
30  
      //print("Searching " + n(l(data), "entry"));
31  
      new L data2;
32  
      new L<Int> rowIndices;
33  
      for i over data: {
34  
        Map<S, O> map = data.get(i);
35  
        if (isTrue(callF(s.rowTester, pat, map))) {
36  
          data2.add(map);
37  
          rowIndices.add(i);
38  
        }
39  
      }
40  
          
41  
      //print("Found " + n(l(data2), "entry"));
42  
      s.lastFiltered = data2;
43  
      s.lastOriginal = data;
44  
      dataToTable(t, data2);
45  
      if (precise) s.lastFiltered = rawTableData(t);
46  
      s.rowIndices = rowIndices;
47  
    }
48  
  });
49  
  
50  
  s.searchPanel = centerAndEast(withLabel(" Search:", s.tfInput), s.scpRightOfSearchPanel);
51  
  JComponent top = s.searchPanel;
52  
  s.panel = boolOptPar(_, 'withMargin) ? northAndCenterWithMargin(top, t) : northAndCenter(top, t);
53  
  ret s;
54  
}

Author comment

Began life as a copy of #1004385

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, elmgxqgtpvxh, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

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/15
Text MD5: 1338a68f0b1e3bc5fc2b6c5cb0ad2d58
Transpilation MD5: c28141429f42c5b430d65e524df9336a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-30 13:00:02
Source code size: 1596 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 507 / 633
Version history: 14 change(s)
Referenced in: [show references]