Javafxnotes 1
Javafxnotes 1
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.
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);
}
}
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);
}
}
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.
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 −
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);
}
}
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;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Text to ListView");
addButton.setOnAction(event -> {
String enteredText = textArea.getText();
if (!enteredText.isEmpty()) {
listView.getItems().add(enteredText);
textArea.clear();
}
});
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.
Set the dimensions of the scroll pane using the setter methods.
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);
}
}
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);
}
}
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.
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);
}
}
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);
}
}
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).
To create a menu −
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);
}
}
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;
@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);