Search code examples
javaswingjframejtextfield

Wait for key before reading from a JTextField


I have a program that will make a JFrame with a JTextField. How do I get it to wait for text to be in the JTextField / the enter key is pressed?

public static void main(String[] args) {
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   int width = (int) screenSize.getWidth();
   int height = (int) screenSize.getHeight();
JFrame frame = new JFrame();
frame.getContentPane().add(new Text());
JTextField field = new JTextField(10);
frame.add(field, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height-55);
frame.setVisible(true);
   // There is a keylistener here called listener. It was 100 lines. Did not want to copy
frame.addKeyListener(listener);
double x = 0;

x = Double.parseDouble(field.getText());


System.out.println(x);

I want

x = Double.parseDouble(field.getText());

to run only when someone types something in the JTextField in the JFrame.


Solution

    1. Don't use a KeyListener
    2. Add a DocumentListener to your JTextField's Document.
    3. Inside of this listener check to see if the Document is empty or not.
    4. If not empty, change the state of your program to allow the calculation, perhaps by enabling or disabling a JButton or Action.

    For example:

    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Document;
    
    public class DisbleButton extends JPanel {
       private JTextField field = new JTextField(10);
       private ButtonAction buttonAction = new ButtonAction();
       private JButton button = new JButton(buttonAction);
    
       public DisbleButton() {
          add(field);
          add(button);
          buttonAction.setEnabled(false);
    
          field.getDocument().addDocumentListener(new FieldDocListener());
       }
    
       private class FieldDocListener implements DocumentListener {
    
          @Override
          public void changedUpdate(DocumentEvent dEvt) {
             testDoc(dEvt);
          }
    
          @Override
          public void insertUpdate(DocumentEvent dEvt) {
             testDoc(dEvt);
          }
    
          @Override
          public void removeUpdate(DocumentEvent dEvt) {
             testDoc(dEvt);
          }
    
          private void testDoc(DocumentEvent dEvt) {
             Document doc = dEvt.getDocument();
             buttonAction.setEnabled(doc.getLength() > 0);
          }
    
       }
    
       private class ButtonAction extends AbstractAction {
          public ButtonAction() {
             super("Press Me");
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             // TODO do calculation here!
    
          }
       }
    
       private static void createAndShowGui() {
          DisbleButton mainPanel = new DisbleButton();
    
          JFrame frame = new JFrame("DisbleButton");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }