Results 1 to 3 of 3
  1. #1
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    44
    Rep Power
    0

    Default Implementing an Event Handler in a Separate File

    Hello-
    I'm learning to create event handlers in Java FX. Specifically, key event handlers.

    All the examples I have looked at show complicated code-with-parentheses-inside-a-larger-class type of stuff which I am having trouble wrapping my head around. I would like to create an event handling class in a separate file, like I used to do with AWT and Swing.

    The documentation defines the interface "EventHandler" like this:
    public interface EventHandler<T extends Event>.

    I'm not really familiar with placing these <> characters in an interface declaration. It reminds me of templates, or of containers. I suppose it is saying that you specify the kind of event you want to handle, such as a KeyEvent.

    I tried creating a class in a separate file, like this: public class KeyHandler<KeyEvent> implements EventHandler. Is that correct? My compiler did not complain.

    Then my handler method: public void handle(KeyEvent e). The compiler complained that I was not overriding handle(Event). So I changed it to handle(Event e). So now I have to do some type casting?

    Obviously I'm confused, and any feedback would be welcome.
    -Scott

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Implementing an Event Handler in a Separate File

    The compiler complained that I was not overriding handle(Event).
    Can you post code that can be compiled to get that error message so we can see what the problem is?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    384
    Rep Power
    4

    Default Re: Implementing an Event Handler in a Separate File

    This setup is very doable. You have to create a class that implements the EventHandler Interface and you have to specify the type of Event, here probably KeyEvent. So your class would probably look something like

    Java FX Code:
    public class Handler implements EventHandler<KeyEvent> {
    @Override
    	public void handle(KeyEvent e) {
    		//doSomething
    		}
    	}
    }
    Since there might be multiple event types in your GUI this setup can get overwhelming quickly.
    I would suggest you create an instance of a HandlerClass that doesn't implement anything and then just pass in the event and deal with it in the handler method.Here is an example:

    Java FX Code:
    public class KeyEventTest extends Application {
    	
    	@Override
    	public void start(Stage primaryStage) {
    		TheHandler handler = new TheHandler(this);
    		VBox vBox = new VBox();
    		Scene scene = new Scene(vBox, 200, 200);
    		
    		scene.setOnKeyPressed(event -> handler.typeSomething(event));
    		
    		primaryStage.setScene(scene);
    		primaryStage.show();	
    	}
    	public static void main(String[] args) {
    		launch(args);
    	}
    }
    Java FX Code:
    public class TheHandler {
    
    	KeyEventTest keyEventTest;
    	public TheHandler(KeyEventTest keyEventTest){
    		this.keyEventTest=keyEventTest;
    	}
    	public void typeSomething(KeyEvent e){
    		
    		System.out.println(e.getCode());
    	}
    }
    quick comment: The DI of KeyEventTest in TheHandler is unnecessary in the example, but I thought you might need it later on.
    Last edited by benji2505; 07-02-2017 at 02:45 PM.

Similar Threads

  1. GUI/Event handler
    By mbschultz97 in forum New To Java
    Replies: 6
    Last Post: 06-15-2015, 07:10 AM
  2. Problem inside of event handler
    By mwr1976 in forum New To Java
    Replies: 1
    Last Post: 11-12-2011, 04:11 AM
  3. event handler issues
    By kd7vea in forum AWT / Swing
    Replies: 11
    Last Post: 04-22-2011, 12:45 AM
  4. event handler not working properly
    By H3rtaherta in forum Java 2D
    Replies: 3
    Last Post: 11-24-2008, 02:39 AM
  5. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •