Developing a Dialog Box - Java JavaFX

Java examples for JavaFX:Dialog

Description

Developing a Dialog Box

Demo Code

import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
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.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

class MyDialog extends Stage {

  public MyDialog(Stage owner, boolean modality, String title) {
    super();/*from   w  w w .j a  v a 2 s  . c o  m*/
    initOwner(owner);
    Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
    initModality(m);
    setOpacity(.90);
    setTitle(title);
    Group root = new Group();
    Scene scene = new Scene(root, 250, 150, Color.WHITE);
    setScene(scene);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(5);
    gridpane.setVgap(5);

    Label mainLabel = new Label("Enter User Name & Password");
    gridpane.add(mainLabel, 1, 0, 2, 1);

    Label userNameLbl = new Label("User Name: ");
    gridpane.add(userNameLbl, 0, 1);

    Label passwordLbl = new Label("Password: ");
    gridpane.add(passwordLbl, 0, 2);

    final TextField userNameFld = new TextField("Admin");
    gridpane.add(userNameFld, 1, 1);

    final PasswordField passwordFld = new PasswordField();
    passwordFld.setText("drowssap");
    gridpane.add(passwordFld, 1, 2);

    Button login = new Button("Change");
    login.setOnAction((ActionEvent event) -> {
      close();
    });
    gridpane.add(login, 1, 3);
    GridPane.setHalignment(login, HPos.RIGHT);
    root.getChildren().add(gridpane);
  }
}

Related Tutorials