You are reading the article How Does Javafx Eventhandler Work updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Does Javafx Eventhandler Work
Introduction to JavaFX EventHandlerThe following article provides an outline for JavaFX EventHandler. In JavaFX, Event Handling is a process which controls an event as well as decides what has to be triggered, when an event occurs. This will be done writing a code called as an event handler that executes when an event occurs. For single node, it can be more than one event handlers. Moreover, one handler can be used for more than 1 event type as well as node.
Start Your Free Software Development Course
How does JavaFX EventHandler Work?Event filter has to be registered for adding it to a node. This is done using the addEventFilter() method of the class Node.
Code:
public void handle(MouseEvent ev) { . . . } }
First, a handler has to be created.
Then, event filter is added using the syntax as shown below.
Syntax:
crc.addEventFilter(MouseEvent.MOUSE_CLICKED , eventHandler ) ;Similarly, event handler can be removed by using the following syntax.
Syntax:
crc. removeEventFilter (MouseEvent.MOUSE_CLICKED , eventHandler ) ; Examples of JavaFX EventHandlerGiven below are the examples of JavaFX EventHandler:
Example #1 import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class EventHandlerSample extends Application { @Override public void start(Stage st) throws Exception { Circle crc = new Circle(200,200,60); crc.setFill(Color.YELLOW); crc.setStroke(Color.RED); Button butn1 = new Button("Start"); butn1.setTranslateX(225); butn1.setTranslateY(300); butn2.setTranslateX(200); butn2.setTranslateY(230); TranslateTransition tr = new TranslateTransition(); tr.setAutoReverse(true); tr.setByX(250); tr.setCycleCount(110); tr.setDuration(Duration.millis(600)); tr.setNode(crc); @Override public void handle(MouseEvent ev) { if(ev.getSource()==butn1) { tr.play(); } if(ev.getSource()==butn2) { tr.pause(); } ev.consume(); } }; Group gp = new Group(); gp.getChildren().addAll(crc , butn1 , butn2 ) ; Scene sc = new Scene(gp , 520 , 400 ,Color.BLACK); st.setScene(sc); st.setTitle("EventHandler Sample Program"); st.show(); } public static void main(String[] args) { launch(args); } }Output:
On executing the code, it can be seen that a yellow circle gets displayed with two buttons as shown in the above figure.
Example #2JavaFX program to rotate rectangle using event handler.
Code:
import javafx.animation.RotateTransition; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Box; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class EventHandlerSample extends Application { @Override public void start(Stage st) { Box b = new Box(); b.setWidth(160.0); b.setHeight(160.0); b.setDepth(110.0); b.setTranslateX(360); b.setTranslateY(160); b.setTranslateZ(60); txt.setFont(Font.font(null, FontWeight.BOLD, 20)); txt.setFill(Color.RED); txt.setX(30); txt.setY(60); PhongMaterial mt = new PhongMaterial(); mt.setDiffuseColor(Color.RED); b.setMaterial(mt); RotateTransition rt = new RotateTransition(); rt.setDuration(Duration.millis(2000)); rt.setNode(b); rt.setAxis(Rotate.Y_AXIS); rt.setByAngle(360); rt.setCycleCount(50); rt.setAutoReverse(false); TextField txtt = new TextField(); txtt.setLayoutX(60); txtt.setLayoutY(110); @Override public void handle(KeyEvent event) { rt.play(); } }; txtt.addEventHandler(KeyEvent.KEY_TYPED, evnt); @Override public void handle(javafx.scene.input.MouseEvent e) { rt.stop(); } }; b.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, evntt); Group gp = new Group(b, txtt, txt); Scene sc = new Scene(gp, 650, 350 ); PerspectiveCamera cm = new PerspectiveCamera(false); cm.setTranslateX(0); cm.setTranslateY(0); cm.setTranslateZ(0); sc.setCamera(cm); st.setTitle("Event Handlers Sample program"); st.setScene(sc); st.show(); } public static void main(String args[]) { launch(args); } }Output:
In this program, rectangle starts rotating when a letter is typed and stops when another letter is typed.
Recommended Articles
This is a guide to JavaFX EventHandler. Here we discuss the introduction, how does JavaFX EventHandler work? and examples. You may also have a look at the following articles to learn more –
You're reading How Does Javafx Eventhandler Work
Update the detailed information about How Does Javafx Eventhandler Work on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!