I was just writing a quick little application and I wanted to do something special whenever the user released the enter key inside a TextArea. You might be tempted to do it like this:
final TextArea editor = new TextArea(); editor.setOnKeyReleased(new EventHandler<KeyEvent>() { public void handle(KeyEvent t) { if (t.getCode() == KeyCode.ENTER) { System.out.println(editor.getText()); } } });
Can anybody spot the problem in this code? It correctly gets the key event, and it only prints out the text if the code of the key event is ENTER. The problem is that it also executes this code if you typed ALT+ENTER, or CTRL+ENTER, or any other combination of modifiers with the key code! JavaFX 2.0 contains a handy little class called KeyCombination which will handle all the annoying checking of key modifiers + key code to see if a specific key event matches. Here is the more correct version:
final TextArea editor = new TextArea(); editor.setOnKeyReleased(new EventHandler<KeyEvent>() { final KeyCombination combo = new KeyCodeCombination(KeyCode.ENTER); public void handle(KeyEvent t) { if (combo.match(t)) { System.out.println(editor.getText()); } } });
One extra line of code, and you get correct matching of the key events. Sweet!
Today i was fighting with a text area but without sucess. I need use a text area like a console. when a message comes (jms in the case), I need to update the text area with the message content. But the text area is only update on the method end. Do you have any clue of how i can do this?
Thanks!!!
@Maycon,
I’m afraid I don’t understand the problem. You might want to post to https://forums.oracle.com/forums/forum.jspa?forumID=1385 with a short, concise, code example that demonstrates the problem.
Sorry about the bad explanation ( and the bad english). Let me try again: https://forums.oracle.com/forums/thread.jspa?threadID=2298778&stqc=true
how to detect whether user has typed SHIFT + A or CAPS LOCK + A