I am trying to create an event for the button that captures the text entered in the TextField and PasswordField control. I know it has something to do with event-handling, but don't know how to implement it. You will see in my code the class I've started with called AddHandler. I am also trying to build a string that will greet and include the user and pass in the greeting label control in the new label that is marked empty (position 4 of my pane).
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class UserAndPass extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
// Place nodes in the pane
pane.add(new Label("Please enter your username and password: "), 0, 0);
pane.add(new Label("Username: "), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(new TextField(), 1, 2);
Button btAdd = new Button("Enter");
pane.add(btAdd, 1, 3);
// Create and register the handler
btAdd.setOnAction(new AddHandler());
GridPane.setHalignment(btAdd, HPos.RIGHT);
pane.add(new Label(""), 0, 4);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Login Display"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
class AddHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
AddHandler.enter();
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
To access textfields and label from your handler, make them fields.
public class InnerClasssTest extends Application {
private TextField usernameField = new TextField();
private TextField passwordField = new TextField();
private Label greetingLabel = new Label("");
pane.add(new Label("Please enter your username and password: "), 0, 0);
pane.add(new Label("Username: "), 0, 1);
pane.add(usernameField, 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(passwordField, 1, 2);
Button btAdd = new Button("Enter");
pane.add(btAdd, 1, 3);
// Create and register the handler
btAdd.setOnAction(new AddHandler());
GridPane.setHalignment(btAdd, HPos.RIGHT);
pane.add(greetingLabel, 0, 4);
class AddHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
greetingLabel.setText(String.format(
"Hello %s@%s.", usernameField.getText(), passwordField.getText()));
}
}
EDIT: Sorry i misread it, you need to create the fields first like @monolith52 allready said
Button randomButton = new Button("Click Me");
Than you can use Lambda expressions to do what you want.
Here as a Single line: randomButton .setOnAction(e -> Do here what you want );
And as multi Line:
randomButton .setOnAction(e -> {
*Do here what you want*
});
Then you simple have to get the strings from your fields with:
userNameField.getText()
and
passwordField.getText()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.