JavaFX - Create Beautiful Email Form


JavaFX Create Email Form

This is a practical tutorial in which we see how to create a beautiful email sending form using the following controls:

  1. ComboBox - Custom editable and non-editable comboboxes.
  2. TextField - To display the email subject.
  3. Labels - To show various captions and notification when email is sent.
  4. TextArea - To allow user to type the email message.
  5. Button - To simulate sending email when clicked.

Let's start.

Video Tutorial

Here's the video tutorial:

Demo

JavaFX Email Form

EmailForm.java

This is the only file and class we need.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class EmailForm extends Application {
    final Button sendBtn = new Button("Send");
    final Label notification = new Label();
    final TextField subject = new TextField("");
    final TextArea text = new TextArea("");

    String address = " ";

    /**
     * The JavaFX Application's start() method. The enty point to JavaFX appllication.
     */
    @Override
    public void start(Stage stage) {
        stage.setTitle("Email Form App");
        Scene scene = new Scene(new Group(), 500, 270);

        final ComboBox emailComboBox = new ComboBox();
        emailComboBox.getItems().addAll("[email protected]", "[email protected]",
                "[email protected]", "[email protected]", "[email protected]");

        emailComboBox.setPromptText("Email address");
        emailComboBox.setEditable(true);
        emailComboBox.setOnAction((Event ev) -> {
            address = emailComboBox.getSelectionModel().getSelectedItem().toString();
        });

        final ComboBox priorityComboBox = new ComboBox();
        priorityComboBox.getItems().addAll("Highest", "High", "Normal", "Low", "Lowest");
        priorityComboBox.setValue("Normal");
        priorityComboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                final ListCell<String> cell = new ListCell<String>() {
                    {
                        super.setPrefWidth(100);
                    }

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);
                            if (item.contains("High")) {
                                setTextFill(Color.RED);
                            } else if (item.contains("Low")) {
                                setTextFill(Color.GREEN);
                            } else {
                                setTextFill(Color.BLACK);
                            }
                        } else {
                            setText(null);
                        }
                    }
                };
                return cell;
            }

        });

        //simulate sending the email
        sendBtn.setOnAction((ActionEvent e) -> {
            if (emailComboBox.getValue() != null && !emailComboBox.getValue().toString().isEmpty()) {
                notification.setText("Your message was successfully sent" + " to " + address);
                emailComboBox.setValue(null);
                if (priorityComboBox.getValue() != null && !priorityComboBox.getValue().toString().isEmpty()) {
                    priorityComboBox.setValue(null);
                }
                subject.clear();
                text.clear();
            } else {
                notification.setText("Please select a recipient first!");
            }
        });

        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setHgap(10);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("To: "), 0, 0);
        grid.add(emailComboBox, 1, 0);
        grid.add(new Label("Priority: "), 2, 0);
        grid.add(priorityComboBox, 3, 0);
        grid.add(new Label("Subject: "), 0, 1);
        grid.add(subject, 1, 1, 3, 1);
        grid.add(text, 0, 2, 4, 1);
        grid.add(sendBtn, 0, 3);
        grid.add(notification, 1, 3, 3, 1);

        Group root = (Group) scene.getRoot();
        root.getChildren().add(grid);
        stage.setScene(scene);
        stage.show();

    }
    /**
     * Main method. Launch our JavaFX application here.
     */
    public static void main(String[] args) {
        launch(args);
    }

}

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post