Sunday, August 17, 2014

Using JavaFX to create a Sign in form [JavaFX]

This program shows how to create a simple sign-in form using JavaFX.

Source:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LoginPractice extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Text welcomeText = new Text();
        welcomeText.setText("Welcome");
        welcomeText.setFont(Font.font("Tahoma", FontWeight.MEDIUM, 20));

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        grid.add(welcomeText, 0, 0, 2, 1);

        Label username = new Label();
        username.setText("Username");
        grid.add(username, 0, 1);

        TextField usernamefield = new TextField();
        grid.add(usernamefield, 1, 1);

        Label password = new Label();
        password.setText("Password");
        grid.add(password, 0, 2);

        PasswordField passwordfield = new PasswordField();
        grid.add(passwordfield, 1, 2);

        Button button = new Button("Sign In"); 
        HBox hbox = new HBox(8); // spacing = 8
        hbox.getChildren().add(button);
        hbox.setAlignment(Pos.BOTTOM_RIGHT);
        grid.add(hbox, 1, 3);
        //grid.setGridLinesVisible(true); //Uncomment to see the actual layout
        final Text pressedText = new Text();
        pressedText.setFill(Color.FIREBRICK);
        grid.add(pressedText, 1, 5);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                pressedText.setText("Signing you in...");
            }
        });

        Scene scene = new Scene(grid, 300, 250);
        stage.setScene(scene);
        stage.setTitle("Login");
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

No comments:

Post a Comment