Java – let actionlistener listen for changes in jtextfield instead of just entering?
•
Java
So you may know that if you have a text field and add an actionlistener, it will only listen to the buttons However, I want my actionlistener to listen to the changes in the text So basically I have this:
public static JPanel mainPanel() { JPanel mainp = new JPanel(); JTextArea areap = new JTextArea("Some text in the textarea"); JTextField fieldp = new JTextField("Edit this"); areap.setEditable(false); fieldp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(//change in textfield,for instance a letterpress or space bar) { //Do this } } }); mainp.add(areap); mainp.add(fieldp); return mainp; }
In any way, can I listen to changes in the text (as recorded in the actionperformed event)?
Solution
Reply from @ jrl
Use basic documents:
myTextField.getDocument().addDocumentListener();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码