Trending September 2023 # A Complete Guide On Javafx Event # Suggested October 2023 # Top 9 Popular | Lifecanntwaitvn.com

Trending September 2023 # A Complete Guide On Javafx Event # Suggested October 2023 # Top 9 Popular

You are reading the article A Complete Guide On Javafx Event updated in September 2023 on the website Lifecanntwaitvn.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 October 2023 A Complete Guide On Javafx Event

Introduction to JavaFX Event

Using JavaFX, several types of applications such as desktop, web, and graphical applications can be developed. Most of the applications in the current world need user interaction to work. For that, the event concept is used from JavaFX. An event is said to have happened in the situations where the user interacts with the application nodes. These events can be triggered using mouse movements, button press, page scrolling, etc. That is, these events are able to give a notification that something has happened from the user end.

Start Your Free Software Development Course

Syntax of JavaFX Event

There are several events that JavaFX supports. For Event class, the package used is javafx.event which is considered as the base class.

Following are the different types of events supported by JavaFX:

Represented Class: MouseEvent.

Syntax:

2. KeyEvent: This event occurs in the situation where a keystroke happens at the node.

Represented Class: KeyEvent.

Actions: Typing key, pressing a key, releasing the key.

Syntax:

3. DragEvent: This event occurs in the situation where dragging of the mouse is done.

Represented Class: DragEvent.

Actions: Entering drag, dropping drag, entering target, exiting target, drag over.

Syntax:

Represented Class: WindowEvent.

Actions: Hiding window, showing window.

Syntax:

How JavaFX Event Handling works?

Event Handling is the process in which the decision to determine what has to have happened when an event occurs and how to control that particular event.

For this, a code is used as an event handler that gets executed at the time which the event has occurred.

JavaFX offers several handlers as well as filters for handling the events.

That is, for every event in JavaFX, it has a target which is the node where the event has occurred (these nodes can be scene, window, or node), a source where the event has generated (mouse, keys, etc.), type of the event (mouse event, key event, etc.).

Constructors of JavaFX Event

Given below are the two constructors of the JavaFX event:

Methods of JavaFX Event

Given below are the different methods:

clone(): A copy of the event will be created and returned.

consume(): Notes the event as consumed.

getEventType(): Type of the event will be returned.

getTarget(): Target of the event will be returned.

isConsumed(): Checks the event is consumed using any filter or handler.

copyFor(Objectsrc, EventTarget target): A copy of the event will be created and returned with the source and target mentioned.

fireEvent(EventTargettrgt, Event ev): Mentioned event will be fired.

Example of JavaFX Event

Given below is the sample program of the event handler in JavaFX:

JavaFX program to demonstrate the event handling.

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 EventProgramSample extends Application{ @Override public void start(Stage st) throws Exception { Circle crc = new Circle(150,150,60); crc.setFill(Color.RED); crc.setStroke(Color.GREEN); buttn.setTranslateX(120); buttn.setTranslateY(200); Button btn1 = new Button("wait.."); btn1.setTranslateX(176); btn1.setTranslateY(220); TranslateTransition tobj = new TranslateTransition(); tobj.setAutoReverse(true); tobj.setByX(220); tobj.setCycleCount(120); tobj.setDuration(Duration.millis(600)); tobj.setNode(crc); @Override public void handle(MouseEvent ev) { if(ev.getSource()==buttn) { tobj.play(); } if(ev.getSource()==btn1) { tobj.pause(); } ev.consume(); } }; Group gp = new Group() ; gp.getChildren().addAll(crc,buttn,btn1) ; Scene sc = new Scene(gp,430,320,Color.BLUE) ; st.setScene(sc); st.setTitle("Sample on EvenetHandler"); st.show(); } public static void main(String[] args) { launch(args); } }

Output:

Conclusion

JavaFX offers different types of applications such as desktop, web, and graphical applications. As user interaction is one of the major steps in the applications, certain event handlers are used. In this article, different aspects such as syntax, types, constructors, methods, working, and examples of an event in JavaFX is given.

Recommended Articles

This is a guide to JavaFX Event. Here we discuss the introduction, how JavaFX event handling works? constructors, methods and example. You may also have a look at the following articles to learn more –

You're reading A Complete Guide On Javafx Event

Update the detailed information about A Complete Guide On Javafx Event on the Lifecanntwaitvn.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!