Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:38:03+00:00 2026-05-28T06:38:03+00:00

I have picked a basic example of printing Hello World on screen when the

  • 0

I have picked a basic example of printing “Hello World” on screen when the mouse is clicked The code goes like this.

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author gauravp
 */
public class Sample extends Application {

    /**
     * @param args the command line arguments
     */
    Button btn = new Button("ok");
    //Label l = new Label("Done");

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("First Stage");


        //Created anonymous inner class EventHandler<ActionEvent>
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                    System.out.print("Hello World !!");


            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

In documentation it is mentioned that EventHandler is an interface , but how come the interface be instantiated…

“new EventHandler<ActionEvent>()”

In a lot of confusion….please reply if you have any idea.. Here is the link
for the EventHandler interface :
http://docs.oracle.com/javafx/2.0/api/javafx/event/EventHandler.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:38:04+00:00Added an answer on May 28, 2026 at 6:38 am

    The syntax

        new EventHandler<ActionEvent>() {
            @Override // <- notice the annotation, it overrides from the interface.
            public void handle(ActionEvent event) {
                    System.out.print("Hello World !!");
            }
        }
    

    creates an “anonymous inner class” that implements EventHandler, and defines the handle method. If you inspect the classes generated when you compile your project, you will probably find a class file named Sample$1 (or similar) which is the class generated for this code.

    You can read up on inner (anonymous) classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

    To answer your question: EventHandler is an interface, and this code doesn’t actually create an instance of it, but an instance of the newly declared anonymous class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have picked up this example which converts BitSet to Byte array. public static
I have this bit of code that get the image i have picked from
EDIT : This question doesn't really make sense once you have picked up what
I have a simple java project with this basic structure: IncludeMe src deploy siteSpecific
I have been programming Java for around 15 years. While this is a basic
The first language I learnt was PHP, but I have more recently picked up
I have a rails app that has picked up a bit of traction, but
I have been slowly learning SQL the last few weeks. I've picked up all
Warning : I have just picked up Maven, so things mentioned might be wrong
Hey, in a tutorial C++ code, I found this particular piece of confusion: PlasmaTutorial1::PlasmaTutorial1(QObject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.