Event Handling - Mouse events

 
Event Handling - Mouse events 


// JavaFX controls and event handling 

// Mouse events - moved and clicked


import javafx.application.*;

import javafx.stage.*;

import javafx.scene.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.event.*;

import javafx.geometry.*;


public class  EventHandling_Demo extends Application

{     


   public static void main(String[] as)

   {

        // Start the JavaFX application by calling launch().

        launch(as);

   }


   // Override the start() method.

   public void start(Stage myStage)

   {           

        // Give the stage a title.

        myStage.setTitle("JavaFx - Mouse Event handling");


        // Use a FlowPane for the root node.

        FlowPane fp = new FlowPane(Orientation.VERTICAL,25,25);


        // Center the controls in the scene.

        fp.setAlignment(Pos.CENTER);

       

        // Creating a heading and response text

        Label L1 = new Label("Event Handling - Mouse Events.");


        Label L2 = new Label("");

Label L3 = new Label("");

      

        // Add to the scene graph.

        fp.getChildren().addAll(L1, L2, L3);

     

        // Create a scene.

        Scene myScene = new Scene(fp, 300, 100);


        // Set the scene on the stage.

        myStage.setScene(myScene); 

       

        // Event Handling       

        myScene.setOnMouseMoved(new EventHandler<MouseEvent>(){

             public void handle(MouseEvent me)

             {  

                 L2.setText("Mouse Coordinates : ("+me.getSceneX() + ", "+me.getSceneY()+")");

             }

        } );

   

        myScene.setOnMouseClicked(new EventHandler<MouseEvent>(){

             public void handle(MouseEvent me)

             {  

                 L3.setText(me.getButton()+ " Button Clicked "+me.getClickCount() + " times.");

             }

        } ) ;


        // Show the stage and its scene.

        myStage.show();

   }

}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu