JavaFX এ Dialogs এবং Alerts ব্যবহার করে আপনি ইউজার ইন্টারঅ্যাকশন ও তথ্য প্রদানের জন্য পপ-আপ উইন্ডো তৈরি করতে পারেন। Dialogs এবং Alerts সাধারণত সতর্কবার্তা, কনফার্মেশন, ইনপুট গ্রহণ, এবং সাধারণ তথ্য প্রদর্শনের জন্য ব্যবহৃত হয়।
JavaFX Dialogs এবং Alerts
- Alerts: এটা একটি সহজ ধরনের ডায়ালগ উইন্ডো যা ইউজারের কাছে কিছু সতর্কবার্তা বা তথ্য প্রদর্শন করে।
- Dialogs: এটি সাধারণত Alert এর থেকে একটু বেশি কাস্টমাইজযোগ্য এবং ইনপুট গ্রহণ করতে সক্ষম।
1. Alerts:
Alert ক্লাসটি সাধারণত ইউজারের কাছে একটি সতর্কবার্তা, তথ্য বা কনফার্মেশন জানাতে ব্যবহৃত হয়। এর মাধ্যমে আপনি CONFIRMATION, INFORMATION, WARNING, ERROR, ইত্যাদি টাইপের সতর্কবার্তা প্রদর্শন করতে পারেন।
Alert এর উদাহরণ:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class AlertExample extends Application {
@Override
public void start(Stage primaryStage) {
// Information Alert তৈরি করা
Alert infoAlert = new Alert(AlertType.INFORMATION);
infoAlert.setTitle("Information");
infoAlert.setHeaderText("Information Header");
infoAlert.setContentText("This is an information alert.");
// Show the information alert
infoAlert.showAndWait();
// Warning Alert তৈরি করা
Alert warningAlert = new Alert(AlertType.WARNING);
warningAlert.setTitle("Warning");
warningAlert.setHeaderText("Warning Header");
warningAlert.setContentText("This is a warning alert.");
// Show the warning alert
warningAlert.showAndWait();
// Error Alert তৈরি করা
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Error");
errorAlert.setHeaderText("Error Header");
errorAlert.setContentText("This is an error alert.");
// Show the error alert
errorAlert.showAndWait();
// Layout এবং Scene সেট করা
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX Alerts Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ব্যাখ্যা:
- AlertType.INFORMATION: এটি সাধারণত সাধারণ তথ্য প্রদর্শন করতে ব্যবহৃত হয়।
- AlertType.WARNING: এটি সতর্কবার্তা প্রদর্শন করে।
- AlertType.ERROR: এটি ত্রুটি বার্তা প্রদর্শন করে।
showAndWait()মেথডটি আলার্টটি দেখানোর পর, ইউজার যখন আলার্টটি বন্ধ করে তখন প্রোগ্রাম আবার চালু হয়।
2. Confirmation Alert:
Alert এর CONFIRMATION টাইপটি ব্যবহার করে ইউজার থেকে কোনো সিদ্ধান্ত গ্রহণ করা যেতে পারে, যেমন "Yes" বা "No" বাটন।
Confirmation Alert উদাহরণ:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ConfirmationAlertExample extends Application {
@Override
public void start(Stage primaryStage) {
// Confirmation Alert তৈরি করা
Alert confirmationAlert = new Alert(AlertType.CONFIRMATION);
confirmationAlert.setTitle("Confirmation");
confirmationAlert.setHeaderText("Do you want to continue?");
confirmationAlert.setContentText("Click Yes to continue or No to exit.");
// Show the confirmation alert and wait for a response
ButtonType result = confirmationAlert.showAndWait().orElse(ButtonType.CANCEL);
if (result == ButtonType.YES) {
System.out.println("User clicked YES");
} else if (result == ButtonType.NO) {
System.out.println("User clicked NO");
} else {
System.out.println("User cancelled");
}
// Layout and Scene setup
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Confirmation Alert Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ব্যাখ্যা:
- ButtonType.YES এবং ButtonType.NO ব্যবহার করে ইউজারের উত্তর যাচাই করা হয়।
showAndWait()ইউজারের ক্লিক করার পর, ফলাফল (Yes, No, Cancel) নেয় এবং তার ভিত্তিতে সিদ্ধান্ত নেওয়া হয়।
3. TextInputDialog (ইনপুট গ্রহণ):
TextInputDialog ব্যবহার করে ইউজার থেকে ইনপুট নেওয়া যায়। এটি একটি সাধারণ ডায়ালগ যা টেক্সট ইনপুট ফিল্ড সহ একটি ডায়ালগ উইন্ডো প্রদর্শন করে।
TextInputDialog উদাহরণ:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.Optional;
public class TextInputDialogExample extends Application {
@Override
public void start(Stage primaryStage) {
// TextInputDialog তৈরি করা
TextInputDialog inputDialog = new TextInputDialog();
inputDialog.setTitle("Input Dialog");
inputDialog.setHeaderText("Please enter your name:");
inputDialog.setContentText("Name:");
// ইউজারের ইনপুট গ্রহণ করা
Optional<String> result = inputDialog.showAndWait();
// যদি ইউজার কিছু ইনপুট দেয়, তবে তা প্রদর্শন করা
result.ifPresent(name -> System.out.println("Hello, " + name));
// Layout and Scene setup
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Text Input Dialog Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ব্যাখ্যা:
- TextInputDialog ব্যবহার করে ইউজারের কাছ থেকে টেক্সট ইনপুট নেওয়া হয়।
showAndWait()মেথডটি ইউজারের ইনপুট বা ডায়ালগ বন্ধ হওয়ার পর ফলাফল ফেরত দেয়।
4. FileChooser Dialog:
FileChooser ডায়ালগ ব্যবহার করে আপনি ফাইল নির্বাচন করতে পারবেন, যা ইউজারকে একটি ফাইল পছন্দ করার সুযোগ দেয়।
FileChooser উদাহরণ:
import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import java.io.File;
public class FileChooserExample extends Application {
@Override
public void start(Stage primaryStage) {
// FileChooser তৈরি করা
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt"));
// Show open file dialog
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
System.out.println("File selected: " + file.getAbsolutePath());
}
// Layout and Scene setup
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("FileChooser Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ব্যাখ্যা:
- FileChooser ইউজারকে একটি ফাইল সিলেক্ট করার জন্য একটি ডায়ালগ উইন্ডো প্রদর্শন করে।
showOpenDialog()মেথডটি ইউজারকে একটি ফাইল নির্বাচন করার সুযোগ দেয়।
সারাংশ:
- Alert ডায়ালগ সাধারণত ইউজারকে সতর্কবার্তা, তথ্য, বা কনফার্মেশন প্রদর্শন করতে ব্যবহৃত হয়।
- TextInputDialog ইউজার থেকে ইনপুট নিতে ব্যবহৃত হয়।
- FileChooser ইউজারের কাছ থেকে ফাইল নির্বাচন করার জন্য ব্যবহৃত হয়।
- Dialogs এবং Alerts আপনাকে JavaFX অ্যাপ্লিকেশনে সহজেই ইউজার ইন্টারঅ্যাকশন এবং ডায়ালগ উইন্ডো তৈরি করতে সহায়তা করে।
Read more