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

21
LINES

< > BotCompany Repo | #1033917 // setComboBoxItems_notifyListeners - fire a change event when a different item is selected afterwards

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

Libraryless. Click here for Pure Java version (4862L/27K).

static <A> void setComboBoxItems_notifyListeners(JComboBox<A> cb, Cl<A> items) {
  if (cb != null) swing {
    if (eq(getComboBoxItems(cb), items)) ret; // nothing to do
    
    var sel = getSelectedItem_typed(cb);
    setComboBoxItems(cb, items);
    var item = getSelectedItem_typed(cb);
    if (!eq(sel, item)) {
      // restore old selection if possible
      if (sel != null && contains(items, sel))
        setSelectedItem(cb, sel);
      else {
        //printVars("combobox", +sel, +item);
      
        // force onChange event(s) (need to use two events)
        setSelectedItem(cb, (A) null);
        setSelectedItem(cb, item);
      }
    }
  }
}

Author comment

Began life as a copy of #1009086

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033917
Snippet name: setComboBoxItems_notifyListeners - fire a change event when a different item is selected afterwards
Eternal ID of this version: #1033917/9
Text MD5: eee6ae28a8cadf69d053a879993056b5
Transpilation MD5: bc52b17668aef67e2b81107d16a8d60f
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-13 16:33:31
Source code size: 680 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 150 / 245
Version history: 8 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)