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

59
LINES

< > BotCompany Repo | #1001788 // onEnter (on JTextField, JButton, JList, JComboBox, JTable, JTextArea) - also selects all on JTextField

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

Libraryless. Click here for Pure Java version (5379L/30K).

static JTextField onEnter(JTextField tf, JButton btn) {
  if (btn != null)
    onEnter(tf, r { clickButton(btn) });
  ret tf;
}

static JTextField onEnter(JTextField tf, O action) {
  if (action == null || tf == null) ret tf;
  tf.addActionListener(actionListener {
    tf.selectAll();
    callF(action);
  });
  ret tf;
}

static JButton onEnter(JButton btn, final O action) {
  if (action == null || btn == null) ret btn;
  btn.addActionListener(actionListener(action));
  ret btn;
}

static JList onEnter(JList list, O action) {
  list.addKeyListener(enterKeyListener(rCallOnSelectedListItem(list, action)));
  ret list;
}

static JComboBox onEnter(final JComboBox cb, Runnable action) {
  swing {
    if (cb.isEditable()) {
      JTextField text = (JTextField) cb.getEditor().getEditorComponent();
      onEnter(text, action);
    } else {
      cb.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter");
      cb.getActionMap().put("enter", abstractAction("", r { cb.hidePopup(); callF(action); }));
    }
  }
  ret cb;
}

static JTable onEnter(final JTable table, fO action) {  
  table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    
  table.getActionMap().put("Enter", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
      callF(action, table.getSelectedRow());
    }
  });
  ret table;
}

/*static JTextArea onEnter(final JTextArea ta, fO action) {
  addKeyListener(ta, enterKeyListener(action));
  ret ta;
}*/

static JTextField onEnter(Runnable action, JTextField tf) {
  ret onEnter(tf, action);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001788
Snippet name: onEnter (on JTextField, JButton, JList, JComboBox, JTable, JTextArea) - also selects all on JTextField
Eternal ID of this version: #1001788/32
Text MD5: 14c5ac516f6e86f6812b2304f056c610
Transpilation MD5: 9dc706b8fdc9d95e9f168942f0408552
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-01 00:37:03
Source code size: 1677 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 788 / 1519
Version history: 31 change(s)
Referenced in: #1001825 - onFocusLeave
#1002427 - Accellerating 629 (SPIKE)
#1005075 - addActionListener - synonym of onEnter for JTextField
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007151 - onEnterAndNow (on JTextField, JButton or JList)
#1013481 - onEnterWithClose (JComboBox)
#1019605 - onEnterAndCtrlEnter
#3000382 - Answer for ferdie (>> t = 1, f = 0)