0% found this document useful (0 votes)
27 views8 pages

JavaFX Programs

The document discusses several JavaFX programs that demonstrate working with common GUI components like text fields, labels, buttons, checkboxes, radio buttons, menus, and mouse interactions. The programs include: 1. A program that accepts two numbers as input in text fields and displays their sum or product when buttons are clicked. 2. Programs that demonstrate checkboxes, radio buttons, and menu bars by printing selected options to the console. 3. Programs that move an ellipse or draw lines based on mouse drag and release events.

Uploaded by

coder ppl
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)
27 views8 pages

JavaFX Programs

The document discusses several JavaFX programs that demonstrate working with common GUI components like text fields, labels, buttons, checkboxes, radio buttons, menus, and mouse interactions. The programs include: 1. A program that accepts two numbers as input in text fields and displays their sum or product when buttons are clicked. 2. Programs that demonstrate checkboxes, radio buttons, and menu bars by printing selected options to the console. 3. Programs that move an ellipse or draw lines based on mouse drag and release events.

Uploaded by

coder ppl
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
You are on page 1/ 8

JavaFX Programs

Working with TextField, Label and Button


2 numbers are given as inputs in two textfields and upon clicking ‘add’, they both are added and
the sum is displayed in textfield 3. Upon clicking ‘multiply’, their product is displayed

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.PerspectiveCamera;
import javafx.collections.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.scene.layout.*;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.scene.transform.Scale;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.beans.value.*;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class JavaFXB1 extends Application


{
TextField tf1,tf2,tf3;

public void start(Stage stage1) throws Exception


{

FlowPane root=new FlowPane();

Label lb1=new Label("a");


Label lb2=new Label("b");
tf1=new TextField();
tf2=new TextField();
tf3=new TextField();
Button b1=new Button("add");
Button b2=new Button("multiply");

b1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent ae)
{

tf3.setText(""+(Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText())));
}
});

b2.setOnAction((ActionEvent ae)->
{
tf3.setText(""+Integer.parseInt(tf1.getText())*Integer.parseInt(tf2.getText()));
}
);
root.getChildren().addAll(lb1,tf1,lb2,tf2,tf3,b1,b2);
Scene scene = new Scene(root, 200, 200);
stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();
}
public static void main(String args[])
{
launch(args);
}
}
Working with CheckBox

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;

public class CheckBoxEx extends Application


{
public void start(Stage stage1) throws Exception
{

FlowPane root=new FlowPane();


CheckBox c1=new CheckBox("SCOPE");
CheckBox c2=new CheckBox("SITE");
root.getChildren().addAll(c1,c2);
root.addEventHandler(ActionEvent.ANY,(ActionEvent ae)->{
if(c1.isSelected()) System.out.println(c1.getText());
if(c2.isSelected()) System.out.println(c2.getText());
});

/* (or) register the event handler individually for each of the checkboxes

c1.addEventHandler(ActionEvent.ANY,(ActionEvent ae)->{
if(c1.isSelected()) System.out.println(c1.getText());
});
c2.addEventHandler(ActionEvent.ANY,(ActionEvent ae)->{
if(c2.isSelected()) System.out.println(c2.getText());
});*/

Scene scene = new Scene(root, 200, 200);


stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();
}
public static void main(String args[])
{
launch(args);
}
}
Working with Radiobuttons

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;

public class RadioButtonEx extends Application


{
public void start(Stage stage1) throws Exception
{

FlowPane root=new FlowPane();


RadioButton r1=new RadioButton("BTech");
RadioButton r2=new RadioButton("MTech");
ToggleGroup tg=new ToggleGroup();
r1.setToggleGroup(tg);
r2.setToggleGroup(tg);
// registering the event handler with the root so that it is invoked when either of
// the radiobuttons are clicked

root.addEventHandler(ActionEvent.ANY,(ActionEvent ae)->
{
RadioButton rb=(RadioButton)tg.getSelectedToggle();
System.out.println(rb.getText());
});

/*
(or) register the event handler individually for each of the radio buttons
r1.setOnAction((ActionEvent ae)->
{
System.out.println(((RadioButton)tg.getSelectedToggle()).getText());
});
r2.setOnAction((ActionEvent ae)->
{
System.out.println(((RadioButton)tg.getSelectedToggle()).getText());
});*/

root.getChildren().addAll(r1,r2);
Scene scene = new Scene(root, 200, 200);
stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();
}
public static void main(String args[])
{
launch(args);
}
}

Working with MenuBar

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;

public class MenuBarEx extends Application


{
public void start(Stage stage1) throws Exception
{
FlowPane root=new FlowPane();
MenuBar mb=new MenuBar();
Menu m1=new Menu("square");
Menu m2=new Menu("circle");
MenuItem mi1=new MenuItem("area");
MenuItem mi2=new MenuItem("perimeter");
MenuItem mi3=new MenuItem("area");
MenuItem mi4=new MenuItem("circumference");
m1.getItems().addAll(mi1,mi2);
m2.getItems().addAll(mi3,mi4);
mb.getMenus().addAll(m1,m2);

mi1.setOnAction((ActionEvent ae)->
{
System.out.println(((MenuItem)ae.getSource()).getText());
});
mi2.setOnAction((ActionEvent ae)->
{
System.out.println(((MenuItem)ae.getSource()).getText());
});
mi3.setOnAction((ActionEvent ae)->
{
System.out.println(((MenuItem)ae.getSource()).getText());
});
mi4.setOnAction((ActionEvent ae)->
{
System.out.println(((MenuItem)ae.getSource()).getText());
});

root.getChildren().addAll(mb);
Scene scene = new Scene(root, 200, 200);
stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();
}
public static void main(String args[])
{
launch(args);
}
}

Working with Mouse: Moving the ball based on mouse movements

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.input.MouseEvent;

public class BallMoveEx extends Application


{
Line newline;
Ellipse c;
double sx,sy,ex,ey;
public void start(Stage stage1) throws Exception
{

Pane root1=new Pane();


c=new Ellipse(100,200,50,50);
c.setFill(Color.MAGENTA);

root1.getChildren().addAll(c);
EventHandler<MouseEvent> me=new EventHandler<MouseEvent>()
{
public void handle(MouseEvent event)
{
c.setCenterX(event.getX());
c.setCenterY(event.getY());
}
};

c.addEventHandler(MouseEvent.MOUSE_PRESSED,me);
c.addEventHandler(MouseEvent.MOUSE_DRAGGED, me);
c.addEventHandler(MouseEvent.MOUSE_RELEASED, me);

Scene scene = new Scene(root1, 600, 400);


stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();

}
public static void main(String args[])
{
launch(args);
}
}

Working with Mouse: Drawing line based on mouse movements

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.input.MouseEvent;

public class LineMoveEx extends Application


{
Line newline;
double sx,sy,ex,ey;
public void start(Stage stage1) throws Exception
{

Pane root1=new Pane();

EventHandler<MouseEvent> me=new EventHandler<MouseEvent>()


{
public void handle(MouseEvent event)
{
sx=event.getX();
sy=event.getY();
ex=event.getX();
ey=event.getY();
newline=new Line(sx,sy,ex,ey);
root1.getChildren().addAll(newline);
}
};

EventHandler<MouseEvent> me2=new EventHandler<MouseEvent>()


{
public void handle(MouseEvent event)
{
sx=ex;
sy=ey;
ex=event.getX();
ey=event.getY();
newline=new Line(sx,sy,ex,ey);
root1.getChildren().addAll(newline);
}
};

root1.addEventHandler(MouseEvent.MOUSE_PRESSED,me);
root1.addEventHandler(MouseEvent.MOUSE_DRAGGED, me2);
root1.addEventHandler(MouseEvent.MOUSE_RELEASED, me2);

Scene scene = new Scene(root1, 600, 400);


stage1.setTitle("Sample application");
stage1.setScene(scene);
stage1.show();

}
public static void main(String args[])
{
launch(args);
}
}

You might also like