In this Java GUI article we are going to learn about Creating Password Field in JavaFX,
so the Password Field class implements a specialized text field. The characters typed by a
user are hidden by displaying an echo string.
Python GUI Development Tutorials with Video Training
- PyQt5 GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development Tutorials
- Kivy GUI Development Tutorials
- wxPython GUI Development Tutorials
This is the complete source code for Java GUI – Creating Password Field in JavaFX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class MyPassField extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("JavaFX Password Field"); HBox hbox = new HBox(); hbox.setPadding(new Insets(10)); hbox.setSpacing(10); PasswordField passwordField = new PasswordField(); Button button = new Button("Show Password"); Label label = new Label(""); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { String password = passwordField.getText(); label.setText(password); } }); hbox.getChildren().addAll(passwordField, button, label); Scene scene = new Scene(hbox, 400,300); stage.setScene(scene); stage.show(); } } |
You can create javafx password field by creating the object of PasswordField class.
1 |
PasswordField passwordField = new PasswordField(); |
This is our HBox layout. we want to add our widgets in this hbox container. also we have used
some padding with spacing for our hbox layout.
1 2 3 |
HBox hbox = new HBox(); hbox.setPadding(new Insets(10)); hbox.setSpacing(10); |
In here we are going to create our widgets, especially we need a password field, button and a label.
1 2 3 |
PasswordField passwordField = new PasswordField(); Button button = new Button("Show Password"); Label label = new Label(""); |
This is our click handler, when the user clicks on the button, we are going to show the
hidden password in the label.
1 2 3 4 5 6 7 |
button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { String password = passwordField.getText(); label.setText(password); } }); |
When you create a widget in JavaFX, for example button,label,combobox or some other
widgets, you need to add that to your container, in here we need to add our password field,
button and label in HBox container, because we want the align the widgets horizontally.
if you want to align widgets vertically you can use VBox layout.
1 |
hbox.getChildren().addAll(passwordField, button, label); |
Also for every JavaFX application we need to create a Scene object. in the scene we need
to add our container with the width and height of the window, if you want to colorize your
window, you can do it in here. you can see that we have given 400 width and 300 height for the
window.
1 |
Scene scene = new Scene(hbox, 400,300); |
At the end you need to set your scene object to the stage of the window, and show the window.
1 2 |
stage.setScene(scene); stage.show(); |
Run the complete code and this is the result.
Subscribe and Get Free Video Courses & Articles in your Email