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

38
LINES

< > BotCompany Repo | #1003557 // bindToComponent - do something on showing/un-showing of component

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

Libraryless. Click here for Pure Java version (9937L/55K).

1  
static <A extends JComponent> A bindToComponent(A component, Runnable onShow, Runnable onUnShow) {
2  
  swing {
3  
    final Var<Bool> flag = new(false);
4  
    component.addAncestorListener(new AncestorListener {
5  
      public void ancestorAdded(AncestorEvent event) {
6  
        if (flag!) print("Warning: bindToComponent logic failure");
7  
        flag.set(true);
8  
        pcallF(onShow);
9  
      }
10  
  
11  
      public void ancestorRemoved(AncestorEvent event) {
12  
        if (!flag!) print("Warning: bindToComponent logic failure");
13  
        flag.set(false);
14  
        pcallF(onUnShow);
15  
      }
16  
  
17  
      public void ancestorMoved(AncestorEvent event) {
18  
      }
19  
    });
20  
    if (component.isShowing()) { // Hopefully this matches the AncestorListener logic
21  
      flag.set(true);
22  
      pcallF(onShow);
23  
    }
24  
  }
25  
  ret component;
26  
}
27  
28  
static <A extends JComponent> A bindToComponent(A component, Runnable onShow) {
29  
  ret bindToComponent(component, onShow, null);
30  
}
31  
32  
// passing a value from onShow to onUnShow
33  
static <A extends JComponent, B> A bindToComponent(A component, IF0<B> onShow, IVF1<B> onUnShow) {
34  
  new Var<B> b;
35  
  ret bindToComponent(component,
36  
    -> b.set(onShow!),
37  
    -> { try { onUnShow.get(b!); } finally { b.set(null); } });
38  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003557
Snippet name: bindToComponent - do something on showing/un-showing of component
Eternal ID of this version: #1003557/11
Text MD5: d968de24ade4872e27f05d7fe1308e2e
Transpilation MD5: 6974a8b4ef539d2f9d33407ad0870c5f
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-20 16:44:29
Source code size: 1255 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 832 / 914
Version history: 10 change(s)
Referenced in: [show references]