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

58
LINES

< > BotCompany Repo | #1000897 // Swing: Single-line text entry

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (582L/5K/15K).

1  
!747
2  
!actionListener {
3  
!x619 // CenteredLine
4  
5  
m {
6  
  static S input = "";
7  
  static S description = "?";
8  
  static boolean ok;
9  
10  
  static JFrame frame;
11  
  static JTextField textField;
12  
  
13  
  static Runnable onOK, onCancel;
14  
  
15  
  p {
16  
    JButton btnOK = new JButton("OK");
17  
    btnOK.addActionListener(actionListener { ok(); });
18  
    JButton btnCancel = new JButton("Cancel");
19  
    btnCancel.addActionListener(actionListener { cancel(); });
20  
    
21  
    JPanel panel = topToBottomPanel();
22  
    textField = new JTextField(input);
23  
    panel.add(new JLabel(description, JLabel.CENTER));
24  
    panel.add(textField);
25  
    textField.addActionListener(actionListener { ok(); });
26  
    
27  
    panel.add(new CenteredLine(btnOK, btnCancel));
28  
29  
    frame = new JFrame("Input | " + description);
30  
    handleEscapeKey(frame);
31  
    
32  
    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
33  
    frame.getRootPane().registerKeyboardAction(actionListener {
34  
      cancel();
35  
    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
36  
37  
    frame.add(panel);
38  
    frame.setBounds(200, 300, 400, 250);
39  
    frame.setVisible(true);
40  
    //exitOnFrameClose(frame); // unless _noExit is set
41  
  }
42  
  
43  
  static void ok() {
44  
    ok = true;
45  
    input = textField.getText();
46  
    print("OK: " + quote(input));
47  
    frame.dispose();
48  
    if (onOK != null) onOK.run();
49  
  }
50  
  
51  
  static void cancel() {
52  
    if (ok) return;
53  
    input = textField.getText();
54  
    print("Cancel with " + quote(input));
55  
    frame.dispose();
56  
    if (onCancel != null) onCancel.run();
57  
  }
58  
}

Author comment

Began life as a copy of #1000834

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, liwcxgsjrgqn, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000897
Snippet name: Swing: Single-line text entry
Eternal ID of this version: #1000897/1
Text MD5: 9bd2d9d9cd4371c433bb0c227e8a3805
Transpilation MD5: c289f692560c22c42dd2f1b073a5cc5d
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-16 17:36:41
Source code size: 1558 bytes / 58 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 603 / 1337
Referenced in: [show references]