0% found this document useful (0 votes)
25 views

Javafxnotes 1

Uploaded by

ranganadh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Javafxnotes 1

Uploaded by

ranganadh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

How to create a Radio Button using JavaFX?

A button is a component, which performs an action (like submit, login, etc..) when
pressed. It is usually labeled with a text or an image specifying the respective
action.

A radio button is a type of button, which is circular in shape. It has two states,
selected and deselected. Generally, radio buttons are grouped using toggle groups,
where you can only select one of them.

You can create a radio button in JavaFX by instantiating the


javafx.scene.control.RadioButton class, which is the subclass of the ToggleButton
class. Action is generated whenever a radio button is pressed or released. You can
set a radio button to a group using the setToggleGroup() method.

Example
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class RadioButtonExample extends Application {
@Override
public void start(Stage stage) {
//Creating toggle buttons
RadioButton button1 = new RadioButton("Java");
RadioButton button2 = new RadioButton("Python");
RadioButton button3 = new RadioButton("C++");
//Toggle button group
ToggleGroup group = new ToggleGroup();
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
//Adding the toggle button to the pane
VBox box = new VBox(5);
box.setFillWidth(false);
box.setPadding(new Insets(5, 5, 5, 50));
box.getChildren().addAll(button1, button2, button3);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Toggled Button Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a check box using JavaFX?

A checkbox is a type of selection control, which is square in shape with a tick


mark int it, It has three states checked or, unchecked and, tristate/indeterminate
(optional). Unlike radio buttons, you cannot combine checkboxes using Toggle
Button.
In JavaFX a checkbox is represented by the javafx.scene.control.CheckBox class.
This class has three boolean properties − allowIndeterminate, indeterminate, and,
selected.

In JavaFX, a checkbox can be −

Checked − The box will be with a tick mark indicating that it is selected. In this
state the value of a selected property is true.

Unchecked − The box will be without a tick mark indicating that it is not selected.
In this state the value of a selected property is false.

Undefined − This is an optional state you have this state by setting the
allowIndeterminate property value to true. In this state (only) the indeterminate
is true indicating the value of the checkbox is undefined.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class CheckBoxExample extends Application {
public void start(Stage stage) {
//Creating the check boxes
CheckBox checkBox1 = new CheckBox("English");
CheckBox checkBox2 = new CheckBox("Hindi");
CheckBox checkBox3 = new CheckBox("Telugu");
CheckBox checkBox4 = new CheckBox("Tamil");
Label label = new Label("Select known languages:");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
label.setFont(font);
//Setting layout
VBox vBox = new VBox(5);
vBox.setPadding(new Insets(5, 5, 5, 50));
vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3, checkBox4);
//Setting the stage
Scene scene = new Scene(vBox, 595, 150, Color.BEIGE);
stage.setTitle("Check Box Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a label using JavaFX?

You can display a text element/image on the User Interface using the Label
component. It is a not editable text control, mostly used to specify the purpose of
other nodes in the application.

In JavaFX, you can create a label by instantiating the javafx.scene.control.Label


class.

Just like a text node you can set the desired font to the text node in JavaFX using
the setFont() method and, you can add color to it using the setFill() method.

To create a label −

Instantiate the Label class.

Set the required properties to it.

Add the label to the scene.

Example
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class LabelExample extends Application {
public void start(Stage stage) {
//Creating a Label
Label label = new Label("Sample label");
//Setting font to the label
Font font = Font.font("Brush Script MT", FontWeight.BOLD,
FontPosture.REGULAR, 25);
label.setFont(font);
//Filling color to the label
label.setTextFill(Color.BROWN);
//Setting the position
label.setTranslateX(150);
label.setTranslateY(25);
Group root = new Group();
root.getChildren().add(label);
//Setting the stage
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Label Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

Example ListView with TextArea

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Text to ListView");

// Create a TextArea for entering text.


TextArea textArea = new TextArea();
textArea.setPrefColumnCount(30);
textArea.setPrefRowCount(5);

// Create a button for adding text to the ListView.


Button addButton = new Button("Add to List");

// Create a ListView to display entered text.


ListView listView = new ListView<>();

addButton.setOnAction(event -> {
String enteredText = textArea.getText();
if (!enteredText.isEmpty()) {
listView.getItems().add(enteredText);
textArea.clear();
}
});

// Create a layout to arrange the components.


HBox inputBox = new HBox(10);
inputBox.getChildren().addAll(textArea, addButton);

VBox root = new VBox(10);


root.getChildren().addAll(inputBox, listView);

// Create the scene and set it in the stage.


Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);

// Set the title of the window.


primaryStage.show();
}
}

How to create a scroll pane using JavaFX?

A scroll pane holds a UI element and provides a scrollable view of it. In JavaFX,
you can create a scroll pane by instantiating the javafx.scene.control.ScrollPane
class. You can set content to the scroll pane using the setContent() method.

To add a scroll pane to a node −

Instantiate the ScrollPane class.

Create the desired node.


Set the node to the scroll pane using the setContent() method.

Set the dimensions of the scroll pane using the setter methods.

Add the scroll pane to the layout pane or group.

Example
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ScrollPaneActionExample extends Application {
public void start(Stage stage) throws FileNotFoundException {
//creating the image object
InputStream stream = new FileInputStream("D:\images\elephant.jpg");
Image image = new Image(stream);
//Creating the image view
ImageView imageView = new ImageView();
//Setting image to the image view
imageView.setImage(image);
//Setting the image view parameters
imageView.setX(5);
imageView.setY(0);
imageView.setFitWidth(595);
imageView.setPreserveRatio(true);
//Creating the scroll pane
ScrollPane scroll = new ScrollPane();
scroll.setPrefSize(595, 200);
//Setting content to the scroll pane
scroll.setContent(imageView);
//Setting the stage
Group root = new Group();
root.getChildren().addAll(scroll);
Scene scene = new Scene(root, 595, 200, Color.BEIGE);
stage.setTitle("Scroll Pane Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a text field using JavaFX?

The text field accepts and displays the text. In the latest versions of JavaFX, it
accepts only a single line. In JavaFX the javafx.scene.control.TextField class
represents the text field, this class inherits the TextInputControl (base class of
all the text controls) class. Using this you can accept input from the user and
read it to your application.

To create a text field, you need to instantiate the TextField class, to the
constructor of this class you can also pass a string value which will be the
initial text of the text field.

Example
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextFieldExample extends Application {
public void start(Stage stage) {
//Creating nodes
TextField textField1 = new TextField("Enter your name");
TextField textField2 = new TextField("Enter your e-mail");
//Creating labels
Label label1 = new Label("Name: ");
Label label2 = new Label("Email: ");
//Adding labels for nodes
HBox box = new HBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(label1, textField1, label2, textField2);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Text Field Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a Toggle Button in JavaFX?

A button is a component, which performs an action (like submit, login, etc..) when
pressed. It is usually labeled with a text or an image specifying the respective
action.

A toggle button indicates whether it is elected or not, using toggle button(s) you
can switch between multiple states. Generally, multiple toggle buttons are grouped
and you can select only one at a time.

You can create a toggle button in JavaFX by instantiating the


javafx.scene.control.ToggleButton class. You can assign a toggle button to a group
using the setToggleGroup() method.

Only one button will be selected in a toggle group, unlike the radio button if you
click on the selected toggle button it will be un-selected.

Example
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ToggledButtonExample extends Application {
@Override
public void start(Stage stage) {
//Creating toggle buttons
ToggleButton button1 = new ToggleButton("Java");
ToggleButton button2 = new ToggleButton("Python");
ToggleButton button3 = new ToggleButton("C++");
//Toggle button group
ToggleGroup group = new ToggleGroup();
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
//Adding the toggle button to the pane
VBox box = new VBox(5);
box.setFillWidth(false);
box.setPadding(new Insets(5, 5, 5, 50));
box.getChildren().addAll(button1, button2, button3);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Toggled Button Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a TreeView using JavaFX?

A tree provides a view of hierarchical structures, each tree contains a root


(highest object) and it contains children. You can create a tree view by
instantiating the javafx.scene.control.TreeView class.

Example
The following Example demonstrates the creation of a TreeView.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TreeViewExample extends Application {
public void start(Stage stage) {
//Creating tree items
TreeItem root1 = new TreeItem("Programming Languages");
TreeItem item1 = new TreeItem("Java");
TreeItem item2 = new TreeItem("Python");
TreeItem item3 = new TreeItem("C++");
//Adding elements to root1
root1.getChildren().addAll(item1, item2, item3);
TreeItem root2 = new TreeItem("NoSQL Databases");
TreeItem item4 = new TreeItem("Neo4j");
TreeItem item5 = new TreeItem("HBase");
TreeItem item6 = new TreeItem("Cassandra");
//Adding elements to root2
root2.getChildren().addAll(item4, item5, item6);
TreeItem root3 = new TreeItem("Bigdata & Analytics");
TreeItem item7 = new TreeItem("Hadoop");
TreeItem item8 = new TreeItem("Mahout");
TreeItem item9 = new TreeItem("Hive");
//Adding elements to root2
root3.getChildren().addAll(item7, item8, item9);
//list View for educational qualification
TreeItem<String> base = new TreeItem<String>("Technologies");
base.setExpanded(true);
base.getChildren().addAll(root1, root2, root3);
//Creating a TreeView item
TreeView view = new TreeView(base);
view.setPrefHeight(300);
VBox pane = new VBox(10);
pane.setPadding(new Insets(5, 5, 5, 50));
pane.getChildren().addAll(view);
//Setting the stage
Group node = new Group(pane);
Scene scene = new Scene(node, 595, 320, Color.BEIGE);
stage.setTitle("List View Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

How to create a Menu using JavaFX?

A menu is a list of options or commands presented to the user. In JavaFX a menu is


represented by the javafx.scene.control.Menu class, you can create a menu by
instantiating this class.

While instantiating, you can pass the title of the menu as a parameter to its
constructor. The Menu class contains an observable list that holds the contents of
the menu (menu items).

Menu items and menu bar


The menu items are represented by the javafx.scene.control.MenuItem class, a
superclass of the Menu class. You can display a text or a graphic as a menu item
and add the desired cation to it.

To create a menu −

Instantiate the Menu class.

Create a required number of menu items by instantiating the MenuItem class.

Add the created menu items to the observable list of the menu.

The javafx.scene.control.MenuBar class represents a menu bar that holds all the
menus in a UI application.
Example
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MenuExample extends Application {
public void start(Stage stage) {
//Creating a menu
Menu fileMenu = new Menu("File");
//Creating menu Items
MenuItem item1 = new MenuItem("Add Files");
MenuItem item2 = new MenuItem("Start Converting");
MenuItem item3 = new MenuItem("Stop Converting");
MenuItem item4 = new MenuItem("Remove File");
MenuItem item5 = new MenuItem("Exit");
//Adding all the menu items to the menu
fileMenu.getItems().addAll(item1, item2, item3, item4, item5);
//Creating a menu bar and adding menu to it.
MenuBar menuBar = new MenuBar(fileMenu);
menuBar.setTranslateX(200);
menuBar.setTranslateY(20);
//Setting the stage
Group root = new Group(menuBar);
Scene scene = new Scene(root, 595, 200, Color.BEIGE);
stage.setTitle("Menu Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

Drawing Basic Shapes

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;

public class FxCanvasExample2 extends Application


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

@Override
public void start(Stage stage)
{
// Create the Canvas with a width of 400 px and a height of 200 px.
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Set line width
gc.setLineWidth(2.0);
// Set fill color
gc.setFill(Color.RED);
// Draw a rounded Rectangle
gc.strokeRoundRect(10, 10, 50, 50, 10, 10);
// Draw a filled rounded Rectangle
gc.fillRoundRect(100, 10, 50, 50, 10, 10);
// Change the fill color
gc.setFill(Color.BLUE);
// Draw an Oval
gc.strokeOval(10, 70, 50, 30);
// Draw a filled Oval
gc.fillOval(100, 70, 50, 30);
// Draw a Line
gc.strokeLine(200, 50, 300, 50);
// Draw an Arc
gc.strokeArc(320, 10, 50, 50, 40, 80, ArcType.ROUND);
// Draw a filled Arc
gc.fillArc(320, 70, 50, 50, 00, 120, ArcType.OPEN);

// Create the Pane


Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");

// Add the Canvas to the Pane


root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Drawing Basic Shapes on a Canvas");
// Display the Stage
stage.show();
}
}

You might also like