Wa0018.
Wa0018.
txqmplt
9) s/21
lo) Explqin
Q.4 (a) Compare JavaFX with Swing and AWT
Introduced In JDK 1.0 (1995) JDK 1.2 (1998) JDK 8 (2014, fully integrated)
platform)
Layout Management Simple layouts Complex layout Flexible layout panes (VBox,
managers HBox, etc.)
1. Event Source
The event source is the component (Ul element) that generates an event.
" Example: A Button, TextField , or Slider that the user interacts with.
Example:
java O Copy Edit
2. Event Object
" The event object is an instance of a class that describes the event.
" It contains information like event type, time, source, etc.
" Common classes:
Example:
java Copy Edit
btn.setonAction(e -> {
|/ 'e' is theevent object (ActionEvent)
System.out. println("Sour ce: " + e.getSource());
):
btn.setOnAction(e -> {
System.out. println("Button Clicked");
btn.setOnAction(new EventHandler<ActionEvent>() {
@0verride
public void handle(ActionEvent e) {
System.out .println( "Handled by anonymous class") ;
);
Simple JavaFX Event Program
java Copy Edit
@0verride
How to Run:
javac SimpleEventDemo.java
java SimpleEventDemo
Let me know if youwant this program using anonymous inner class or with custom event
handler class.
b) Discuss JavaFX benefits?
1. Rich UI Features
2. CSS Styling
JavaFX supports CSS-like styling for Ul components.
This makes it easy to separate design from logic and create visually appealing
applications.
3. FXML Support
" Ul can be designed using FXML (XML-based) files.
" Developers can design the layout using tools like Scene Builder without writing Java
code.
4. Property Binding
" JavaFX allows data binding, so Ul elements automatically update when data changes.
Useful for building reactive interfaces.
5. Hardware Acceleration
" Uses GPU-based rendering for better performance.
" Smooth animations, transitions, and multimedia playback.
7. Cross-Platform
. JavaFX applications run on Windows, macoS, Linux, and mobile platfoms (via third
party tools like Gluon).
9. MVC Support
" Promotes clean architecture using Model-View Controller(MVC) pattern, especialy
with FXML.
Basic Structure:
@0verride
public void start(Stage primaryStage) {
11 Create a button
Button btn = new Button("Click Me");
1/ Main method
public static void main(String[] args) {
launch(args); // Launch JavaFX application
Explanation:
Part Description
start(Stage primaryStage ) Main entry point; sets up the Ul and shows the stage
(window).
What is JavaFX?
JavaFX isa modern Java GUI (Graphical User Interface) framework used to develop rich
desktop applications. It provides powerful features for building interactive and visually
appealing Uls using:
Scene Graph architecture
" CSS styling
" FXML for layout design
" Built-in animation and multimedia support
Graphics Architecture Uses Scene Graph and GPU Uses native OS components
acceleration (heavyweight)
UIControls Rich controls (charts, media, Basic controls only (Button, Label,
WebView, etc.) TextField)
Q.5(b)Architecture of JavaFX
JavaFX architecture is designed to create rich and efficient GUl applications using a
modular and layered approach. It mainly consists of the following components:
2. Scene Graph
" Core part of JavaFX.
" Atree-like structure where each element (node) is a part of the UI.
" Nodes include:
4. Prism
5. Quantum Toolkit
Connects JavaFX Application Thread with Prism and Glass.
" Manages the coordination between UI, rendering, and window events.
6. Media Engine
" Supports playing audio and video.
" Based on GStreamer (a multimedia framework).
7. Web Engine
" Supports rendering HTML content using WebView.
" Buitt on top of WebKit (browser engine).
JavaFX Application
| (Controls, Layouts, UI)
Scene Graph |
Quantum Toolkit
| Prism | Glass
| (GPU) | | Windowing|
+.
(b) Demonstrate animation effect in JavaFX
@0verride
What it does:
" Creates a circle.
Defined inside a class but outside any method, like a regular member.
2. Static Nested Class
A static class defined inside another class. It doesn't have access to instance
members of the outer class.
void methodWithLocalClass() {
| 3. Local Inner Class
class Local Inner {
void display() {
System.out- printIn ("Local Inner Class");
void methodWithAnonymousClass() {
1| 4. Anonymous Inner Class implement:ing Runnable
Runnabler = new Runnable() {
public void run() {
System.out. println("Anonymous Inner Class");
r.run();
Output:
Pgeql O Copy Edit
1. Adapter Classes
" Adapter classes provide empty implementations of event listener interfaces.
" Useful when you want to handle only some events of an interface without
implementing all methods.
For example, MouseListener has 5 methods; if you want to implement just one, you
can extend MouseAdapter and override only the needed methods.
2. Mouse Events
" Mouse events include actions like:
mouseClicked
"mousePressed
"mouseReleased
"mouseEntered
"mouseExited
" These events happen when the user interacts with the mouse on a component.
Here's a short Java program using MouseAdapter to handle mouse clicks on a label:
import javax.swing. *;
import java. awt.event.*;
});
add(label);
setSize(300, 150);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setVisible (true );
What is MVC?
MVC is a software design pattern that separates an application into three interconnected
components:
1. Model:
-+
Controller |
Advantages of MVC
" Separation of concerns: Clear division of Ul, data, and control logic.
Reusability: Models can be reused with different views.
Disadvantages of MVC
" Complexity: Increases complexity for simple applications.
" Learning curve: Developers must understand the pattern well.
Overhead: More files and communication between components.
" Tight coupling risk: If not properly designed, Controller and View may become tightly
cOupled.
(b) Explain Inner class with exampl
1| Inner class
class Inner Class {
void display() {
System.out . println(message); // Accessing outer class member
Output:
Pgsql O Copy Edit
Explanation:
" InnerClass is defined inside OuterClass.