Event Handling - Keyboard Events

 

Event Handling - Keyboard Events


// JavaFX controls 

// Keyboard Event handling


import javafx.application.*; 

import javafx.scene.*; 

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*; 

import javafx.scene.control.*; 

import javafx.scene.input.*;

import javafx.geometry.*;


public class EventHandling_KB extends Application 

{

public static void main(String[] args)

{

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

launch(args);

}


// Override the start() method. 

public void start(Stage myStage) 

{

StringBuffer str = new StringBuffer();

// Give the stage a title. 

myStage.setTitle("JavaFx - Keyboard 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 heading = new Label("Keyboard Event Handling.");

Label respText = new Label("");

// Add to the scene graph. 

fp.getChildren().addAll(heading, respText);

// Create a scene.

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


// Set the scene on the stage. 

myStage.setScene(myScene);

// Event Handling

myScene.setOnKeyTyped(new EventHandler<KeyEvent>(){

public void handle(KeyEvent ke) 

{

str.append(ke.getCharacter());

respText.setText(str.toString());

}

} ) ;

// Show the stage and its scene. 

myStage.show();

}

}


// Sample Output


>javac --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls EventHandling_KB.java

>java --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls EventHandling_KB 







JavaFX Application

 

JavaFX Application


// JavaFX Application - Factorial Calculator

// Layout management - GridPane


import javafx.application.*; 

import javafx.scene.*; 

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*; 

import javafx.scene.control.*;

import javafx.scene.input.*; 

import javafx.scene.text.*;

import javafx.geometry.*;


public class JavaFX_Demo extends Application

{

public static void main(String []args)

{

launch(args);

}

public void init(){}

public void stop(){}

public void start(Stage myStage)

{

myStage.setTitle("JavaFX Application");

GridPane gp = new GridPane();

gp.setHgap(50);

gp.setVgap(50);

gp.setPadding(new Insets(50,50,50,50));

Label L1 = new Label("Enter Number : ");

L1.setFont(new Font("Times New Roman Bold",15));

Label L2 = new Label("Factorial : ");

L2.setFont(new Font("Times New Roman Bold",15));

TextField tf1 = new TextField();

tf1.setPrefColumnCount(20);

tf1.setMinHeight(25);

TextField tf2 = new TextField();

tf2.setPrefSize(20,25);

tf1.setFont(new Font("Times New Roman Bold",10));

tf2.setFont(new Font("Times New Roman Bold",10));

Button B1 = new Button("Clear");

Button B2 = new Button("Submit");

Button B3 = new Button("Exit");

B1.setFont(new Font("Times New Roman Bold Italic",15));

B2.setFont(new Font("Times New Roman Bold Italic",15));

B3.setFont(new Font("Times New Roman Bold Italic",15));

B1.setPrefWidth(75);

B2.setPrefWidth(75);

B3.setPrefWidth(75);

gp.add(L1,0,0);

gp.add(L2,0,1);

gp.add(tf1,1,0);

gp.add(tf2,1,1);

gp.add(B1,0,2);

HBox hb = new HBox(75);

hb.getChildren().addAll(B2,B3);

gp.add(hb,1,2);

B1.setOnAction(new EventHandler<ActionEvent>()

{

public void handle(ActionEvent ae)

{

tf1.setText("Enter Number.");

tf2.setText("");

tf1.setStyle("-fx-background-color: pink; -fx-text-fill: red;");

}

});

B2.setOnAction(new EventHandler<ActionEvent>()

{

public void handle(ActionEvent ae)

{

int n = Integer.parseInt(tf1.getText());

long fact=1;

for(int i=1;i<=n;i++)

fact=fact*i;

tf2.setText(Long.toString(fact));

}

});

B3.setOnAction(new EventHandler<ActionEvent>()

{

public void handle(ActionEvent ae)

{

myStage.close();

}

});

tf1.setOnMouseClicked(new EventHandler<MouseEvent>()

{

public void handle(MouseEvent me)

{

tf1.setStyle("-fx-background-color: white; -fx-text-fill: black;");

tf1.clear();

}

});

Scene myScene = new Scene(gp, 500, 300);

myStage.setScene(myScene);

myStage.show();

}

}


// Sample Output


>javac --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls JavaFX_Demo.java

>java --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls JavaFX_Demo








JavaFX Layout Management


 JavaFX Layout Management 


// JavaFX Controls

// Layout management 


import javafx.application.*; 

import javafx.scene.*; 

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*; 

import javafx.scene.control.*;

import javafx.scene.input.*; 

import javafx.geometry.*;


public class Layout_Demo extends Application

{


public static void main(String []as)

{

launch(as);

}

public void init(){}

public void stop(){}

public void start(Stage myStage)

{

myStage.setTitle("Layout Demo");

Button B1 = new Button("Top");

Button B2 = new Button("Right");

Button B3 = new Button("Bottom");

Button B4 = new Button("Left");

Button B5 = new Button("Center");

// Flow Layout Horizontal Orientation

FlowPane layout = new FlowPane(Orientation.HORIZONTAL, 25, 35);

layout.setAlignment(Pos.CENTER);

layout.getChildren().addAll(B1,B2,B3,B4,B5);

layout.setPadding(new Insets(10,10,10,10));

Scene myScene = new Scene(layout, 250, 250);

myStage.setScene(myScene);

myStage.show();

}

}


/*

// Flow Layout Vertical Orientation

FlowPane layout = new FlowPane(Orientation.VERTICAL, 25, 35);

layout.setAlignment(Pos.CENTER);

layout.getChildren().addAll(B1,B2,B3,B4,B5);

layout.setPadding(new Insets(10,10,10,10));


// HBox Layout

HBox layout = new HBox(25);

layout.setAlignment(Pos.CENTER);

layout.getChildren().addAll(B1,B2,B3,B4,B5);

layout.setPadding(new Insets(10,10,10,10));


// VBox Layout

VBox layout = new VBox(15);

layout.setAlignment(Pos.CENTER);

layout.getChildren().addAll(B1,B2,B3,B4,B5);

layout.setPadding(new Insets(10,10,10,10));


// Stack layout

B1.setPrefSize(200,200);

B2.setPrefSize(175,175);

B3.setPrefSize(150,150);

B4.setPrefSize(125,125);

B5.setPrefSize(100,100);

StackPane layout = new StackPane();

layout.setAlignment(Pos.CENTER);

layout.getChildren().addAll(B1,B2,B3,B4,B5);

layout.setPadding(new Insets(10,10,10,10));


// Border Layout

BorderPane layout = new BorderPane();

// Adding controls to each location

layout.setTop(B1);

layout.setRight(B2);

layout.setBottom(B3);

layout.setLeft(B4);

layout.setCenter(B5);

// Set alignment in each location 

BorderPane.setAlignment(B1,Pos.TOP_CENTER);

BorderPane.setAlignment(B2,Pos.CENTER_RIGHT);

BorderPane.setAlignment(B3,Pos.BOTTOM_CENTER);

BorderPane.setAlignment(B4,Pos.CENTER_LEFT);

BorderPane.setAlignment(B5,Pos.CENTER);


// Grid layout

GridPane layout = new GridPane();

layout.setAlignment(Pos.CENTER);

layout.setHgap(50);

layout.setVgap(50);

layout.setPadding(new Insets(25,25,25,25));

// Adding controls to each location (col x row)

layout.add(B1,0,0);

layout.add(B2,0,1);

layout.add(B3,1,0);

layout.add(B4,1,1);

// Column span (2,1)

layout.add(B5,0,2,2,1);

GridPane.setHalignment(B5, HPos.CENTER);

*/


// Sample Output


>javac --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls Layout_Demo.java

>java --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls Layout_Demo 


// FlowPane - Horizontal Orientation


// FlowPane - Vertical Orientation


// HBox


// VBox


// StackPane


// BorderPane



// GridPane








Generics and Arrays - Sorting


Generics and Arrays - Sorting 


// Generics and arrays

// Sorting


class Generics<T extends Comparable<T>>

{

T []x;

public Generics(T []Arr)

{

x = Arr;

}


void fnDisplay()

{

System.out.print("\nArray elements : ");

for(int i = 0; i<x.length;i++)

System.out.print(x[i]+"  ");

}


void fnSort()

{

T Tmp;

for(int i = 0;i<x.length-1;i++)

for(int j = i+1; j<x.length;j++)

{

if(x[i].compareTo(x[j])>0)

{

Tmp = x[i];

x[i] = x[j];

x[j] = Tmp;

}

}

}

}


class Generics_Demo

{

public static void main(String as[])

{

Integer []A1 = {1,3,5,4,2};

Generics<Integer> obj1 = new Generics<Integer>(A1);

obj1.fnDisplay();

obj1.fnSort();

obj1.fnDisplay();


String []A2 = {"IT", "CSE", "ECE", "AUTO"};

Generics<String> obj2 = new Generics<String>(A2);

obj2.fnDisplay();

obj2.fnSort();

obj2.fnDisplay();


Double []A3 = {7.3, 1.2,4.2,3.6,0.8};

Generics<Double> obj3 = new Generics<Double>(A3);

obj3.fnDisplay();

obj3.fnSort();

obj3.fnDisplay();

}

}


// Sample Output

>javac Generics_Demo.java
>java Generics_Demo

Array elements : 1  3  5  4  2
Array elements : 1  2  3  4  5

Array elements : IT  CSE  ECE  AUTO
Array elements : AUTO  CSE  ECE  IT

Array elements : 7.3  1.2  4.2  3.6  0.8
Array elements : 0.8  1.2  3.6  4.2  7.3

>

Generic and Inheritance


Generics and Inheritance


 // Generics and Inheritance 


class Rectangle<T extends Number>

{

T l, b;

public Rectangle(T x, T y)

{

l = x;

b = y;

}

void fnArea()

{

System.out.println("Area = "+(l.doubleValue()*b.doubleValue()));

}

}


class Box<T extends Number> extends Rectangle<T>

{

T h;

public Box(T x, T y, T z)

{

super(x, y);

h = z;

}

void fnVolume()

{

System.out.println("Volume = "+(l.doubleValue()*b.doubleValue()*h.doubleValue()));

}

}


class Generics_Demo

{

public static void main(String as[])

{

Box<Integer> obj1 = new Box<Integer>(5,6,2);

obj1.fnArea();

obj1.fnVolume();

Box<Double> obj2 = new Box<Double>(2.1,4.3,6.5);

obj2.fnArea();

obj2.fnVolume();

}

}


// Sample Output


>javac Generics_Demo.java

>java Generics_Demo

Area = 30.0

Volume = 60.0

Area = 9.03

Volume = 58.7

>

File Write

 File Write


// File handling in java

// Read from console and write to file


import java.io.*;


class FileWrite

{

public static void main(String as[]) throws IOException

{

FileWriter fout = null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String s = "";

int i = 0;

try

{

fout = new FileWriter("opt.txt");

while(i<=3)

{

s = br.readLine();

fout.write(s+"\n");

i+=1;

}

System.out.println("File written successfully.");

}

catch(Exception e)

{

System.out.println(e);

}

finally

{

if(fout!=null)

fout.close();

System.out.println("End of pgm.");

}

}

}



File Copy

File Copy


 // File handling in java

// Copy from source file to destination file


import java.io.*;


class FH

{

public static void main(String as[]) throws IOException

{

FileInputStream fin = null;

FileOutputStream fout = null;

int i;

try

{

fin = new FileInputStream("ipt.txt");

fout = new FileOutputStream("opt.txt");

while(true)

{

i = fin.read();

if(i==-1)

break;

fout.write((char)i);

}

System.out.println("File copied successfully.");

}

catch(Exception e)

{

System.out.println(e);

}

finally

{

if(fin!=null)

fin.close();

if(fout!=null)

fout.close();

System.out.println("End of pgm.");

}

}

}

File Read

File Read 


// File handling in java

// Read from file and display in terminal


import java.io.*;


class FileDisplay

{

public static void main(String as[]) throws IOException

{

FileInputStream fin = null;

int i;

try

{

fin = new FileInputStream("ipt.txt");

while(true)

{

i = fin.read();

if(i==-1)

break;

System.out.print((char)i);

}

System.out.println("File displayed successfully.");

}

catch(Exception e)

{

System.out.println(e);

}

finally

{

if(fin!=null)

fin.close();

System.out.println("End of pgm.");

}

}

}