I have a JTextField. I want to call a function when the text in it is changed.
JTextField.
How to do it?
The appropriate listener in Java swing to track changes to JTextField text content is the DocumentListener, which you must add to the JTextField document:
myTextField.getDocument().addDocumentListener(new DocumentListener() { // implement the methods });
Use Key Listener this way
JTextField tf=new JTextField(); tf.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(!(ke.getKeyChar()==27||ke.getKeyChar()==65535))//this section will execute only when user is editing the JTextField { System.out.println("User is editing something in TextField"); } } });
You can use catherine listener
JTextField textField = new JTextField(); textField.addCaretListener(new CaretListener() { @Override public void caretUpdate(CaretEvent e) { System.out.println("text field have changed"); } });
You can add a KeyListener or ActionListener to the field and commit events.
Source: https://habr.com/ru/post/1345350/More articles:Why use clone ()? - c #awakeFromFetch with unmanaged property - objective-cASP.NET MVC Injecting Http / Request / Controller context - asp.net-mvc-3Column_property dynamic property with SQLAlchemy - pythonSending MMS using vCard application on Android devices - androidXcode 4 - “No iOS device connected” with iPod touch 2 - iosNo iPhone OS device connected - xcodeHow to post to twitter with PHP? - phpHow can we detect memory leaks in a COM Interop application? - c #“No iPhone OS device connected” after upgrading to version 4.0 - iphoneAll Articles