2
How to call send message method when key Enter was pressured?
Code
@Override
public void keyPressed(KeyEvent e) {
String messageSent = "User: " + writingTextField.getText();
if(e.getKeyCode() == KeyEvent.VK_ENTER){
writingTextField.setText("");
readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
}
}
See if it helps: Push button by hotkey in Java
– Math
I’ve seen all these sites, the problem is that I have to do by keypressed and not by action
– rrr
Which component are you adding keypressed? I saw no problems in the code. Add the text component you want enter to work.
– user28595
tried to verify the value of e.getKeyCode() ?
– jbrunoxd
It is possible that he should not even be entering the event. Perhaps the most appropriate would be to put the Reader in your Textbox, and not in the form.
– Math
enter is not working at all. When I type into Jtextfield a text click on enter and it doesn’t work, but with mouse it works
– rrr
As @Math said, the listerner should be assigned to Jtextfield. Post the rest of the code for easy analysis.
– user28595
I’ve posted, but it’s weird not to give
– rrr
Rodrigo, you are assigning the keypressed listerner to jbutton, you have to assign it to jtextfield, otherwise it really won’t work.
– user28595
Got it! Thanks @Math
– rrr