কম্পিউটার টিউটোরিয়াল

জাভাতে অটো-কমপ্লিট JComboBox বাস্তবায়ন করা


JComboBox হল JComponent ক্লাসের একটি সাবক্লাস এবং এটি একটি টেক্সট ফিল্ড এবং একটি ড্রপ-ডাউন লিস্ট-এর সংমিশ্রণ যেখানে থেকে ব্যবহারকারী একটি মান বেছে নিতে পারে। ব্যবহারকারী কম্বো বক্সের ওপর অ্যাকশন নিলে JComboBox ActionListener, ChangeListener, এবং ItemListener ইন্টারফেস তৈরি করতে পারে।

JComboBox ক্লাস এক্সটেন্ড করে একটি কম্বো বক্স কাস্টমাইজ করে (AutoCompleteComboBox) আমরা অটো-কমপ্লিট JComboBox বাস্তবায়ন করতে পারি যখন ব্যবহারকারী কীবোর্ড থেকে ইনপুট মান টাইপ করে।

উদাহরণ

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.*; 

public class AutoCompleteComboBoxTest extends JFrame { 
    private JComboBox comboBox; 
    
    public AutoCompleteComboBoxTest() { 
        setTitle("AutoCompleteComboBox"); 
        String[] countries = new String[] {"india", "australia", "newzealand", "england", "germany", "france", "ireland", "southafrica", "bangladesh", "holland", "america"}; 
        comboBox = new AutoCompleteComboBox(countries); 
        add(comboBox, BorderLayout.NORTH); 
        setSize(400, 300); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLocationRelativeTo(null); 
        setVisible(true); 
    } 
    
    public static void main(String []args) { 
        new AutoCompleteComboBoxTest(); 
    } 
} 

// AutoCompleteComboBox বাস্তবায়ন 
class AutoCompleteComboBox extends JComboBox { 
    public int caretPos = 0; 
    public JTextField tfield = null; 
    
    public AutoCompleteComboBox(final Object countries[]) { 
        super(countries); 
        setEditor(new BasicComboBoxEditor()); 
        setEditable(true); 
    } 
    
    public void setSelectedIndex(int index) { 
        super.setSelectedIndex(index); 
        tfield.setText(getItemAt(index).toString()); 
        tfield.setSelectionEnd(caretPos + tfield.getText().length()); 
        tfield.moveCaretPosition(caretPos); 
    } 
    
    public void setEditor(ComboBoxEditor editor) { 
        super.setEditor(editor); 
        if(editor.getEditorComponent() instanceof JTextField) { 
            tfield = (JTextField) editor.getEditorComponent(); 
            tfield.addKeyListener(new KeyAdapter() { 
                public void keyReleased(KeyEvent ke) { 
                    char key = ke.getKeyChar(); 
                    if (!(Character.isLetterOrDigit(key) || Character.isSpaceChar(key))) return; 
                    caretPos = tfield.getCaretPosition(); 
                    String text = ""; 
                    try { 
                        text = tfield.getText(0, caretPos); 
                    } catch (javax.swing.text.BadLocationException e) { 
                        e.printStackTrace(); 
                    } 
                    for (int i = 0; i < getItemCount(); i++) { 
                        String element = (String) getItemAt(i); 
                        if (element.startsWith(text)) { 
                            setSelectedIndex(i); 
                            return; 
                        } 
                    } 
                } 
            }); 
        } 
    } 
}

আউটপুট

জাভাতে অটো-কমপ্লিট JComboBox বাস্তবায়ন করা


  1. জাভাতে স্ক্রোলযোগ্য JPanel কীভাবে বাস্তবায়ন করবেন?

  2. জাভাতে JPanel-এর paintComponent() মেথড কীভাবে বাস্তবায়ন করবেন?

  3. জাভাতে JToggleButton কীভাবে বাস্তবায়ন করবেন?

  4. জাভাতে JWindow ব্যবহার করে স্প্ল্যাশ স্ক্রিন কীভাবে বাস্তবায়ন করবেন?