Custom Dialog তৈরি করা

Dialog এবং Popup উইন্ডো তৈরি - এডাব্লিউটি (AWT) - Java Technologies

362

AWT (Abstract Window Toolkit)-এ Custom Dialog তৈরি করা মানে হলো একটি কাস্টম ডায়লগ উইন্ডো তৈরি করা যা ব্যবহারকারীকে বিশেষ কোন তথ্য বা অপশন দেখানোর জন্য ব্যবহৃত হয়। Java AWT এ Dialog ক্লাসের মাধ্যমে ডায়লগ উইন্ডো তৈরি করা হয় এবং কাস্টমাইজেশন করার জন্য আমরা সাধারণত Dialog ক্লাসের একটি সাবক্লাস ব্যবহার করি।

Custom Dialog তৈরি করার পদক্ষেপ:

  1. Dialog ক্লাস ব্যবহার করা:
    Dialog একটি উইন্ডো যা মূল ফ্রেমের উপর একটি ডায়লগ উইন্ডো দেখায়। এটি সাধারণত একটি মেসেজ বা ইনপুটের জন্য ব্যবহৃত হয়।
  2. Layout সেট করা:
    ডায়লগের মধ্যে যে উপাদানগুলো রাখতে হবে, সেগুলোর লেআউট সঠিকভাবে সেট করতে হবে। এর জন্য FlowLayout, GridLayout, BorderLayout ইত্যাদি ব্যবহার করা যেতে পারে।
  3. Close Button বা অন্যান্য বাটন যোগ করা:
    ডায়লগ উইন্ডোটি বন্ধ করতে একটি ক্লোজ বাটন (যেমন "OK" বা "Cancel") যোগ করা যেতে পারে।

Custom Dialog উদাহরণ:

import java.awt.*;
import java.awt.event.*;

public class CustomDialogExample {
    public static void main(String[] args) {
        Frame frame = new Frame("AWT Custom Dialog Example");

        // Create a button to open the custom dialog
        Button button = new Button("Open Custom Dialog");

        // ActionListener for the button to open the dialog
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CustomDialog dialog = new CustomDialog(frame, "Custom Dialog", true);
                dialog.setSize(300, 200);
                dialog.setVisible(true);
            }
        });

        frame.add(button);
        frame.setSize(400, 300);
        frame.setLayout(new FlowLayout());
        frame.setVisible(true);
        
        // Close the frame when clicked on close button
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

class CustomDialog extends Dialog {
    public CustomDialog(Frame parent, String title, boolean modal) {
        super(parent, title, modal);

        // Set the layout for the dialog
        setLayout(new FlowLayout());
        
        // Create a label and add it to the dialog
        Label label = new Label("This is a custom dialog.");
        add(label);
        
        // Create an OK button and add it to the dialog
        Button okButton = new Button("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();  // Close the dialog when OK is clicked
            }
        });
        add(okButton);
    }
}

ব্যাখ্যা:

  1. Main Frame (Parent Frame):
    এখানে একটি Frame তৈরি করা হয়েছে যেখানে একটি বোতাম রয়েছে। এই বোতামটি ক্লিক করলে একটি কাস্টম ডায়লগ উইন্ডো খোলা হবে।
  2. CustomDialog ক্লাস:
    • CustomDialog একটি Dialog ক্লাসের সাবক্লাস। এটি কাস্টম ডায়লগ উইন্ডো তৈরি করতে ব্যবহৃত হয়।
    • এই ডায়লগের মধ্যে একটি লেবেল এবং একটি "OK" বাটন রয়েছে। "OK" বাটনটি ক্লিক করলে ডায়লগটি বন্ধ হয়ে যাবে।
  3. Modal Dialog:
    Dialog এর তৃতীয় প্যারামিটার true দিলে এটি modal ডায়লগ তৈরি করবে, অর্থাৎ ডায়লগটি ওপেন থাকা অবস্থায় মূল উইন্ডোতে কোনো অ্যাকশন করা যাবে না যতক্ষণ না ডায়লগটি বন্ধ হবে।
  4. Layout:
    ডায়লগের মধ্যে উপাদানগুলোর লেআউট FlowLayout ব্যবহার করা হয়েছে। আপনি এটি পরিবর্তন করে অন্য লেআউটও ব্যবহার করতে পারেন (যেমন GridLayout বা BorderLayout)।

ডায়লগের অন্যান্য বৈশিষ্ট্য:

  • Modal vs Modeless:
    Dialog উইন্ডোটি modal বা modeless হতে পারে। Modal ডায়লগ ব্যবহারকারীকে প্রধান উইন্ডোতে ফিরে আসার জন্য ডায়লগটি বন্ধ করতে বাধ্য করে, যখন Modeless ডায়লগ প্রধান উইন্ডোতে ফিরে আসার অনুমতি দেয়।
  • UI Customization:
    আপনি ডায়লগের মধ্যে অন্যান্য উপাদান যেমন TextField, TextArea, Checkbox, RadioButton ইত্যাদি যোগ করতে পারেন, এবং তাদের উপর ইভেন্ট হ্যান্ডলিংও করতে পারেন।

AWT তে কাস্টম ডায়লগ তৈরি করা সহজ এবং কাস্টমাইজযোগ্য। Dialog ক্লাসের মাধ্যমে আপনি বিভিন্ন ধরনের ডায়লগ তৈরি করতে পারেন এবং এতে প্রয়োজনীয় উপাদান যোগ করতে পারেন, যা আপনার অ্যাপ্লিকেশনটিকে আরও ব্যবহারকারী বান্ধব করে তোলে।

Content added By
Promotion

Are you sure to start over?

Loading...