This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Sunday, 10 June 2018
Saturday, 9 June 2018
JavaFX: Handling scrollbar events
By adding
change listner to scroll bar, you can catch the events of scroll bar.
Ex
scrollBar.valueProperty().addListener((ObservableValue<?
extends Number> ov, Number old_val, Number new_val) -> {
label.setText("Scrollbar value
changed from " + old_val + " to " + new_val);
});
Find the
below working application.
package com.sample.demos; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ScrollBar; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ScrollBarApp extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ScrollBar scrollBar = new ScrollBar(); scrollBar.setMin(0); scrollBar.setOrientation(Orientation.VERTICAL); scrollBar.setPrefHeight(180); scrollBar.setMax(360); scrollBar.setUnitIncrement(30); scrollBar.setBlockIncrement(35); Label label = new Label(); label.setTextFill(Color.RED); HBox hBox = new HBox(30, scrollBar, label); Group root = new Group(); root.getChildren().addAll(hBox); Scene scene = new Scene(root, 500, 300); scrollBar.valueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { label.setText("Scrollbar value changed from " + old_val + " to " + new_val); }); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX scroll bar app Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(ScrollBarApp.class, args); } }
JavaFX: Create scroll bar
How to
create scrollbar?
ScrollBar
class provides default constructor to define scroll bar instance.
Ex
ScrollBar
scrollBar = new ScrollBar();
How to
create horizontal (or) vertical scrollbar?
By setting
the orientation to horizontal (or) vertical, you can create horizontal and
vertical scroll bars.
Ex
ScrollBar verticalScrollBar
= new ScrollBar();
verticalScrollBar.setOrientation(Orientation.VERTICAL);
ScrollBar
horizontalScrollBar = new ScrollBar();
horizontalScrollBar.setOrientation(Orientation.HORIZONTAL);
Find the
below working application.
package com.sample.demos; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollBar; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ScrollBarApp extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ScrollBar verticalScrollBar = new ScrollBar(); verticalScrollBar.setMin(0); verticalScrollBar.setOrientation(Orientation.VERTICAL); verticalScrollBar.setPrefHeight(180); verticalScrollBar.setMax(360); verticalScrollBar.setUnitIncrement(30); verticalScrollBar.setBlockIncrement(35); ScrollBar horizontalScrollBar = new ScrollBar(); horizontalScrollBar.setMin(0); horizontalScrollBar.setOrientation(Orientation.HORIZONTAL); horizontalScrollBar.setPrefHeight(30); horizontalScrollBar.setPrefWidth(300); horizontalScrollBar.setMax(500); horizontalScrollBar.setUnitIncrement(30); horizontalScrollBar.setBlockIncrement(35); HBox hBox = new HBox(30, verticalScrollBar, horizontalScrollBar); Group root = new Group(); root.getChildren().addAll(hBox); Scene scene = new Scene(root, 500, 300); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX scroll bar app Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(ScrollBarApp.class, args); } }
JavaFX: ScrollBar widget
By using
ScrollBar widget, you can create scroll bar panes and views.
Create scroll bar
Handling scrollbar events
JavaFX: PasswordField: store password
JavaFX
provides PasswordField class, it is specialized implementation of text field,
it hides the characters typed by the user.
How to create password filed?
PasswordField
class provides below construtor to create password field.
public
PasswordField()
Ex
PasswordField
password = new PasswordField();
password.setPromptText("Enter
password");
Find the
below working application.
package com.sample.demos; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class LoginForm extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(5); grid.setHgap(5); TextField userName = new TextField(); userName.setPromptText("Enter your name"); GridPane.setConstraints(userName, 0, 0); grid.getChildren().add(userName); PasswordField password = new PasswordField(); password.setPromptText("Enter password"); GridPane.setConstraints(password, 0, 1); grid.getChildren().add(password); Button signInButton = new Button("SignIn"); Button clearButton = new Button("Clear"); HBox hBox = new HBox(10, signInButton, clearButton); signInButton.setOnAction(event -> { String name = userName.getText(); String pwd = password.getText(); System.out.println("User Name : " + name); System.out.println("password : " + pwd); }); clearButton.setOnAction(event -> { userName.clear(); password.clear(); }); GridPane.setConstraints(hBox, 0, 2); grid.getChildren().add(hBox); Scene scene = new Scene(grid, 500, 300); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Form Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(LoginForm.class, args); } }
JavaFX: Clear the text field
TextField
class provides 'clear' method to clear the information entered in text field.
Find the
below working application.
package com.sample.demos; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class LoginForm extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(5); grid.setHgap(5); TextField userName = new TextField(); userName.setPromptText("Enter your name"); GridPane.setConstraints(userName, 0, 0); grid.getChildren().add(userName); TextField password = new TextField(); password.setPromptText("Enter password"); GridPane.setConstraints(password, 0, 1); grid.getChildren().add(password); Button signInButton = new Button("SignIn"); Button clearButton = new Button("Clear"); HBox hBox = new HBox(10, signInButton, clearButton); signInButton.setOnAction(event -> { String name = userName.getText(); String pwd = password.getText(); System.out.println("User Name : " + name); System.out.println("password : " + pwd); }); clearButton.setOnAction(event -> { userName.clear(); password.clear(); }); GridPane.setConstraints(hBox, 0, 2); grid.getChildren().add(hBox); Scene scene = new Scene(grid, 500, 300); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Form Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(LoginForm.class, args); } }
JavaFX: Processing text field data
TextField class
provides getText and setText methods to process text field data.
TestFX.java
Find the below login
form example. Enter user name and password fields in the login form and click
on the SignIn button, you can able to see the entered values in console.
LoginForm.java
package com.sample.demos; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class LoginForm extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(5); grid.setHgap(5); TextField userName = new TextField(); userName.setPromptText("Enter your name"); GridPane.setConstraints(userName, 0, 0); grid.getChildren().add(userName); TextField password = new TextField(); password.setPromptText("Enter password"); GridPane.setConstraints(password, 0, 1); grid.getChildren().add(password); Button button = new Button("SignIn"); Button clear = new Button("Clear"); HBox hBox = new HBox(10, button, clear); button.setOnAction(event -> { String name = userName.getText(); String pwd = password.getText(); System.out.println("User Name : " + name); System.out.println("password : " + pwd); }); clear.setOnAction(event -> { userName.setText(""); password.setText(""); }); GridPane.setConstraints(hBox, 0, 2); grid.getChildren().add(hBox); Scene scene = new Scene(grid, 500, 300); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Form Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(LoginForm.class, args); } }
Subscribe to:
Posts (Atom)




