There is such code:

public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); JTextField t = new JTextField(); frame.add(BorderLayout.NORTH, t); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); t.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String s = t.getText(); } }); } } 

ActionListener monitors JTextField and transfers data only after pressing Enter .
The program has several JTextField fields. How to make the text read, even if the user does not press Enter every time?

Or how to make a form to fill in differently (with data entry fields and a submit button)? By type as it is done in HTML:

 <form> <input type="text"> <input type="text"> <input type="submit"> </form> 
  • textField.getDocument().addDocumentListener(new DocumentListener() just catch these changes. Didn’t consider this option? - Senior Pomidor

1 answer 1

you need to add a DocumentListener . to catch changes.

  JFrame frame = new JFrame(); JTextField t = new JTextField(); frame.add(BorderLayout.NORTH, t); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); t.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { print(); } public void removeUpdate(DocumentEvent e) { print(); } public void insertUpdate(DocumentEvent e) { print(); } public void print() { System.out.println(t.getText()); } }); } 
  • Thank!!! It helped))) - faq700
  • always - Senior Pomidor