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

1  
static JTextField onEnter(JTextField tf, JButton btn) {
2  
  if (btn != null)
3  
    onEnter(tf, r { clickButton(btn) });
4  
  ret tf;
5  
}
6  
7  
static JTextField onEnter(JTextField tf, O action) {
8  
  if (action == null || tf == null) ret tf;
9  
  tf.addActionListener(actionListener {
10  
    tf.selectAll();
11  
    callF(action);
12  
  });
13  
  ret tf;
14  
}
15  
16  
static JButton onEnter(JButton btn, final O action) {
17  
  if (action == null || btn == null) ret btn;
18  
  btn.addActionListener(actionListener(action));
19  
  ret btn;
20  
}
21  
22  
static JList onEnter(JList list, O action) {
23  
  list.addKeyListener(enterKeyListener(rCallOnSelectedListItem(list, action)));
24  
  ret list;
25  
}
26  
27  
static JComboBox onEnter(final JComboBox cb, Runnable action) {
28  
  swing {
29  
    if (cb.isEditable()) {
30  
      JTextField text = (JTextField) cb.getEditor().getEditorComponent();
31  
      onEnter(text, action);
32  
    } else {
33  
      cb.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter");
34  
      cb.getActionMap().put("enter", abstractAction("", r { cb.hidePopup(); callF(action); }));
35  
    }
36  
  }
37  
  ret cb;
38  
}
39  
40  
static JTable onEnter(final JTable table, fO action) {  
41  
  table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
42  
    .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
43  
    
44  
  table.getActionMap().put("Enter", new AbstractAction() {
45  
    public void actionPerformed(ActionEvent e) {
46  
      callF(action, table.getSelectedRow());
47  
    }
48  
  });
49  
  ret table;
50  
}
51  
52  
/*static JTextArea onEnter(final JTextArea ta, fO action) {
53  
  addKeyListener(ta, enterKeyListener(action));
54  
  ret ta;
55  
}*/
56  
57  
static JTextField onEnter(Runnable action, JTextField tf) {
58  
  ret onEnter(tf, action);
59  
}

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: 793 / 1525
Version history: 31 change(s)
Referenced in: [show references]