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

53
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 (19525L/120K).

sclass TableSearcher {
  JTable table;
  JTextField tfInput;
  JComponent searchPanel, panel;
  F2<S, Map, Bool> rowTester;
  L<Int> rowIndices;
  SingleComponentPanel scpRightOfSearchPanel = scp();

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

static TableSearcher tableWithSearcher2(JTable t, O... _) {
  new TableSearcher s;
  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 = centerAndEast(withLabel(" Search:", s.tfInput), s.scpRightOfSearchPanel);
  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  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, 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/12
Text MD5: 4be2eddaf4351d880e0cb49c2d9355c1
Transpilation MD5: c409f20beaac0925ee4a1fd5e62d3517
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-21 15:46:58
Source code size: 1539 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 349 / 407
Version history: 11 change(s)
Referenced in: [show references]