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).

static <A extends JComponent> A bindToComponent(A component, Runnable onShow, Runnable onUnShow) {
  swing {
    final Var<Bool> flag = new(false);
    component.addAncestorListener(new AncestorListener {
      public void ancestorAdded(AncestorEvent event) {
        if (flag!) print("Warning: bindToComponent logic failure");
        flag.set(true);
        pcallF(onShow);
      }
  
      public void ancestorRemoved(AncestorEvent event) {
        if (!flag!) print("Warning: bindToComponent logic failure");
        flag.set(false);
        pcallF(onUnShow);
      }
  
      public void ancestorMoved(AncestorEvent event) {
      }
    });
    if (component.isShowing()) { // Hopefully this matches the AncestorListener logic
      flag.set(true);
      pcallF(onShow);
    }
  }
  ret component;
}

static <A extends JComponent> A bindToComponent(A component, Runnable onShow) {
  ret bindToComponent(component, onShow, null);
}

// passing a value from onShow to onUnShow
static <A extends JComponent, B> A bindToComponent(A component, IF0<B> onShow, IVF1<B> onUnShow) {
  new Var<B> b;
  ret bindToComponent(component,
    -> b.set(onShow!),
    -> { try { onUnShow.get(b!); } finally { b.set(null); } });
}

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: 831 / 912
Version history: 10 change(s)
Referenced in: [show references]