JavaFx | PasswordField
Last Updated :
27 Jun, 2018
PasswordField class is a part of JavaFX package. It is a Text field that masks entered characters (the characters that are entered are not shown to the user). It allows the user to enter a single-line of unformatted text, hence it does not allow multi-line input.
Constructor of the PasswordField class :
- PasswordField(): creates a new PasswordField
(PasswordField inherits TextField so all the methods of TextField can be used here. There are no separate methods for the password field, all are inherited from the text field.)
Below programs illustrate the use of PasswordField class:
- Java program to create a Password field: This program creates a PasswordField indicated by the name b. The PasswordField will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a Title-pane is created, on which addChildren() method is called to attach the PasswordField inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Passwordfield extends Application
{
public void start(Stage s)
{
s.setTitle( "creating Passwordfield" );
PasswordField b = new PasswordField();
TilePane r = new TilePane();
r.getChildren().add(b);
Scene sc = new Scene(r, 200 , 200 );
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
launch(args);
}
}
|
Output:
- Java program to create a passwordfield and add a event handler: This program creates a PasswordField indicated by the name b. We will create a label which will display the password when the enter key is pressed. We will create an event handler that will handle the event of the password field and the event handler would be added to the passwordfield using setOnAction() method. The PasswordField will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a Title-pane is created, on which addChildren() method is called to attach the PasswordField and a label inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Passwordfield_1 extends Application
{
public void start(Stage s)
{
s.setTitle( "creating Passwordfield" );
PasswordField b = new PasswordField();
TilePane r = new TilePane();
Label l = new Label( "no Password" );
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>(){
public void handle(ActionEvent e)
{
l.setText(b.getText());
}
};
b.setOnAction(event);
r.getChildren().add(b);
r.getChildren().add(l);
Scene sc = new Scene(r, 200 , 200 );
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
launch(args);
}
}
|
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/PasswordField.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...