0% found this document useful (0 votes)
3 views16 pages

Java FX

The document provides JavaFX code examples for creating a GUI application that displays an image and a tip calculator. It includes detailed instructions on setting up the GUI components, handling user interactions, and performing arithmetic operations. Additionally, it demonstrates thread management in Java with examples of using the isAlive() and join() methods, as well as creating threads to display messages at different intervals.

Uploaded by

fjyothi57
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Java FX

The document provides JavaFX code examples for creating a GUI application that displays an image and a tip calculator. It includes detailed instructions on setting up the GUI components, handling user interactions, and performing arithmetic operations. Additionally, it demonstrates thread management in Java with examples of using the isAlive() and join() methods, as well as creating threads to display messages at different intervals.

Uploaded by

fjyothi57
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM:

'Without writing any code, build a GUI that display text in label and image
in an ImageView (use JavaFX)
 Create a FileInputStream representing the image you want to
load.
 Instantiate the Image class bypassing the input stream object
created above, as a parameter to its constructor.
 Instantiate the ImageView class.
 Set the image to it by passing above the image object as a
parameter to the setImage() method.
 Set the required properties of the image view using the respective
setter methods.
 Add the image view mode to the group object.

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ImageViewExample extends Application {
public void start(Stage stage) throws IOException {
//creating the image object
InputStream stream = new FileInputStream("D:\images\
[Link]");
Image image = new Image(stream);
//Creating the image view
ImageView imageView = new ImageView();
//Setting image to the image view
[Link](image);
//Setting the image view parameters
[Link](10);
[Link](10);
[Link](575);
[Link](true);
//Setting the Scene object
Group root = new Group(imageView);
Scene scene = new Scene(root, 595, 370);
[Link]("Displaying Image");
[Link](scene);
[Link]();
}
public static void main(String args[]) {
launch(args);
}
}
[Link] a Tip Calculator app using several JavaFX components and learn
how to respond to user interactions with the GUI
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Main extends Application {

private TextField num1Field;

private TextField num2Field;

private Label resultLabel;

public static void main(String[] args) {

launch(args);

@Override
public void start(Stage primaryStage) {

[Link]("Basic Calculator");

GridPane grid = new GridPane();

[Link](10);

[Link](10);

[Link](new Insets(10, 10, 10, 10));

num1Field = new TextField();

[Link]("Enter first number");

[Link](num1Field, 0, 0);

num2Field = new TextField();

[Link]("Enter second number");

[Link](num2Field, 0, 1);

resultLabel = new Label("Result: ");

[Link](resultLabel, 0, 2);

Button addButton = new Button("Add");

[Link](e ->
performOperation('+'));

[Link](addButton, 1, 0);
Button subtractButton = new Button("Subtract");

[Link](e ->
performOperation('-'));

[Link](subtractButton, 2, 0);

Button multiplyButton = new Button("Multiply");

[Link](e ->
performOperation('*'));

[Link](multiplyButton, 1, 1);

Button divideButton = new Button("Divide");

[Link](e ->
performOperation('/'));

[Link](divideButton, 2, 1);

[Link]().addAll(num1Field, num2Field,
resultLabel, addButton, subtractButton, multiplyButton,
divideButton);

Scene scene = new Scene(grid, 300, 200);

[Link](scene);

[Link]();

private void performOperation(char operator) {


String num1Text = [Link]();

String num2Text = [Link]();

if (isValidNumber(num1Text) &&
isValidNumber(num2Text)) {

double num1 = [Link](num1Text);

double num2 = [Link](num2Text);

double result = 0.0;

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

} else {
[Link]("Result:
Error (Division by zero)");

return;

break;

default:

break;

[Link]("Result: " + result);

} else {

[Link]("Result: Invalid Input");

private boolean isValidNumber(String text) {

return [Link]("-?\\d*\\.?\\d+");

Explanation:

The above code tells----

 Import the necessary JavaFX libraries.


 Create a class "Main" that extends "Application" and overrides the
"start()" method.
 Inside the "start()" method:
o Set the application window title to "Basic Calculator."
 Create a GridPane for organizing UI elements with specific spacing and
padding.
 Create two text fields (num1Field and num2Field) for users to enter the
first and second numbers. Set placeholders for user guidance.
 Create a label (resultLabel) to display the calculation result.
 Create four buttons (addButton, subtractButton, multiplyButton, and
divideButton) for addition, subtraction, multiplication, and division. Each
button is configured to trigger a specific action when clicked.
 Use '[Link]' to position these UI elements within the
grid.
 Add all UI elements to the grid using [Link]().addAll(...).
 Create a Scene with the grid layout, specifying the window dimensions
as 300x200.
 Set the created scene in the stage (primaryStage) and show the
window.
 The "performOperation()" method:
o Reads the user input from the text fields.
o Validates the input to ensure it's a valid number.
o Performs the selected operation (addition, subtraction,
multiplication, or division) on the two numbers.
o Updates the 'resultLabel' with the calculated result or an error
message in case of invalid input or division by zero.
 The "isValidNumber()" method checks whether the input text is a valid
number using a regular expression.
Sample Output:
program:

Write a java program to perform is Alive() and join() method

// Java program to Illustrate isAlive() Method

// of Thread class

// Main class extending Thread class

public class oneThread extends Thread {

// Method 1

// run() method for thread

public void run()

// Print statement

[Link]("college ");
// Try block to check for exceptions

try {

// making thread to sleep for 300 nano-seconds

// using sleep() method

[Link](300);

// Catch block to handle InterruptedException

catch (InterruptedException ie) {

// Display message when exception occurred

[Link]("for college ");

// Method 2

// Main driver method

public static void main(String[] args)

// Creating threads using above class as

// it is extending Thread class

oneThread c1 = new oneThread();

oneThread c2 = new oneThread();


// Starting threads

[Link]();

[Link]();

// Checking whether thread is alive or not

// Returning boolean true if alive else false

[Link]([Link]());

[Link]([Link]());

output:
college
true
true
college
forcollege
forcollege
Program:Write a JAVA program that creates threads by extending
Thread class. First thread display “Good Morning “every 1 sec, the
second thread displays “Hello “every 2 seconds and the third display
“Welcome” every 3 seconds, (Repeat the same by implementing
Runnable)

import [Link].*;
class One extends Thread
{
public void run()
{
for(int i=0;i<100;i++)
{
try{
[Link](1000); }
catch(InterruptedException e){
[Link](e); }
[Link]("Good Morning");
}
}
}
class Two extends Thread
{
public void run()
{
for(int i=0;i<100;i++)
{
try{
[Link](2000); }
catch(InterruptedException e)
{
[Link](e);
}
[Link]("Hello ");
}
}
}
class Three implements Runnable
{
public void run()
{
for(int i=0;i<100;i++)
{
try{
[Link](3000); }
catch(InterruptedException e){
[Link](e); }
[Link]("Wel come");
}
}
}
class ThreadEx
{
public static void main(String[] args)
{
One t1=new One();
Two t2=new Two();
Three tt=new Three();
Thread t3=new Thread(tt);
[Link]("One");
[Link]("Two");
[Link]("Three");
[Link](t1);
[Link](t2);
[Link](t3);
Thread t=[Link]();
[Link](t);
[Link]();[Link]();[Link]();
}
}

Output:
D:\>javac [Link]
D:\>java ThreadEx
Thread[One,5,main]
Thread[Two,5,main]
Thread[Three,5,main]
Thread[main,5,main]
Good Morning
Good Morning
Hello
Wel come
Good Morning
Hello
Good Morning
Good Morning
Hello
Wel come
Good Morning
Good Morning
Hello
Good Morning

You might also like