Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use KeyEvent in JavaFX project?

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?

like image 483
APro Avatar asked Jan 16 '15 11:01

APro


People also ask

How to handle events in JavaFX?

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.

What is a key event type?

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.

Is it necessary to generate a key typed event before key release?

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).

What is source and target in JavaFX?

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.


1 Answers

You have few issues with your code :

  1. You are using onKeyTyped instead of onKeyPressed. For more information visit this link

  2. 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);
    }
}
like image 95
ItachiUchiha Avatar answered Sep 21 '22 12:09

ItachiUchiha