I have searched for a long time for how to write a KeyEvent
to allow my Button clicks by ENTER key
. Note that I'm using JavaFX and FXML
files.
The problem is that when set in the onKeyTyped text field in the FXML file, the FXML Files doesn't accept it. It says Handle method not found
. It just accept ActionEvent
method so I have tried this code:
@FXML
private void key (KeyEvent evt) throws IOException{
if (evt.getCode() == KeyEvent.VK_ENTER){
String az = text1.getText();
//c.1
if(az.contains("1")){
String hh = text11.getText();
Socket socket = null;
InetSocketAddress isa = new InetSocketAddress (hh,80);
}
}
}
So please can anybody help me?
JavaFX provides handlers and filters to handle events. In JavaFX every event has −. Target − The node on which an event occurred. A target can be a window, scene, and a node. Source − The source from which the event is generated will be the source of the event. In the above scenario, mouse is the source of the event.
Common supertype for all key event types. This event occurs when a key has been pressed. This event occurs when a key has been released. This event occurs when a character-generating key was typed (pressed and released). The event contains the character field containing the typed string, the code and text fields are not used.
Key releases are not usually necessary to generate a key typed event, but there are some cases where the key typed event is not generated until a key is released (e.g., entering ASCII sequences via the Alt-Numpad method in Windows).
In JavaFX every event has − Target − The node on which an event occurred. A target can be a window, scene, and a node. Source − The source from which the event is generated will be the source of the event. In the above scenario, mouse is the source of the event.
You have few issues with your code :
You are using onKeyTyped
instead of onKeyPressed
. For more information visit this link
You are most probably using java.awt.event.KeyEvent
, which will not work with JavaFX events
. Try to use javafx.scene.input.KeyEvent
.
The reason I came to this conclusion is because JavaFX doesn't support KeyEvent.VK_ENTER
but instead have KeyCode.ENTER
A concrete example is shown below, you can use the same to convert it into FXML:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ButtonExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = new BorderPane();
Button button = new Button("Press Me!");
pane.setCenter(button);
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
System.out.println("Enter Pressed");
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With