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

!747
!actionListener {
!x619 // CenteredLine

m {
  static S input = "";
  static S description = "?";
  static boolean ok;

  static JFrame frame;
  static JTextField textField;
  
  static Runnable onOK, onCancel;
  
  p {
    JButton btnOK = new JButton("OK");
    btnOK.addActionListener(actionListener { ok(); });
    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(actionListener { cancel(); });
    
    JPanel panel = topToBottomPanel();
    textField = new JTextField(input);
    panel.add(new JLabel(description, JLabel.CENTER));
    panel.add(textField);
    textField.addActionListener(actionListener { ok(); });
    
    panel.add(new CenteredLine(btnOK, btnCancel));

    frame = new JFrame("Input | " + description);
    handleEscapeKey(frame);
    
    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    frame.getRootPane().registerKeyboardAction(actionListener {
      cancel();
    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    frame.add(panel);
    frame.setBounds(200, 300, 400, 250);
    frame.setVisible(true);
    //exitOnFrameClose(frame); // unless _noExit is set
  }
  
  static void ok() {
    ok = true;
    input = textField.getText();
    print("OK: " + quote(input));
    frame.dispose();
    if (onOK != null) onOK.run();
  }
  
  static void cancel() {
    if (ok) return;
    input = textField.getText();
    print("Cancel with " + quote(input));
    frame.dispose();
    if (onCancel != null) onCancel.run();
  }
}

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: 602 / 1335
Referenced in: [show references]