Implements search funcationality for finding JCobobox items with their current index in java swing.

Implements search funcationality for finding JCobobox items with their current index in java swing.

JComboBox shows a popup menu that shows a list and the user can select a option from that specified list.

Today we will create search funcationality for finding JCombobox existing items with their current index

For that we will use some java swing components , Listener Interface , Event , Methods Etc..

  1.    setText() : Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.
  2.     getText() : returns the text from the single-line text field.
  3.     setFont() : sets the font and style when displaying text strings.
  4.     setBounds(): set the position and size of any component.
  5.     add() : add a component on another component or JFrame.
  6.     setLocation(): Sets the location of the point to the specified location.
  7.     setLayout() : sets the layout manager for the component.
  8.     setResizable(): Indicates whether this frame is resizable by the user. By default, all frames are initially resizable. return : true if the user can resize this frame; false otherwise.
  9.      setVisible() : sets the visibility of the component or JFrame. It is by default false.
  10.     addKeyListener Iterface and keyReleased method : Generated when input is received from keyword.
  11.     equals() method : Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
  12.     getItemCount() method : Returns the number of items in the combobox.
  13.     setSelectedIndex(int anindex) method : Selects the item at index anIndex.
  14.     getSelectedIndex() method : Returns the integer value indicating the currently selected index in this combobox.

Example :

package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class comboboxItemSearch extends JFrame {
    //JFrame compponents declraions start here  :
    String empName[]={"ram","raj","rohan","jay","vijay"};
    JComboBox combobox = new JComboBox(empName);
    JLabel mainLabel = new JLabel();
    JLabel disMsg =  new JLabel();
    JTextField txtSearchBox =  new JTextField();

    //Create a constructor
    comboboxItemSearch(){
        //display message by default visible false
        disMsg.setVisible(false);

        //add combobox in JFrame
        add(combobox);
        combobox.setBounds(279, 35, 201, 34);

        //add main label "Search :" in JFrame
        mainLabel.setFont(new Font("Verdana Pro Semibold", 1, 18));
        mainLabel.setText("Search :");
        add(mainLabel);
        mainLabel.setBounds(191, 129, 82, 23);

        //add display message label in JFrame
        disMsg.setFont(new Font("Verdana Pro Cond Semibold", 1, 18));
        add(disMsg);
        disMsg.setBounds(150, 273, 480, 23);

        //keyListener for txtSearchBox
        txtSearchBox.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent evt) {
                if(txtSearchBox.getText().equals("")) {
                    disMsg.setText("Please enter employee name");
                }else {
                    for (int i = 0; i < combobox.getItemCount(); i++) {
                        if (txtSearchBox.getText().equals(combobox.getItemAt(i))) {
                            combobox.setSelectedIndex(i);
                            int index = combobox.getSelectedIndex();
                            disMsg.setText("Employee are exists and there current index number is : " + index);
                            disMsg.setVisible(true);
                            break;
                        } else {
                            disMsg.setText("Employee are not exists");
                            disMsg.setVisible(true);
                        }
                    }
                }
            }
        });
        //add search box in JFrame
        add(txtSearchBox);
        txtSearchBox.setBounds(279, 124, 201, 38);
        txtSearchBox.setFont(new Font("Verdana Pro Semibold", 1, 18));

        //Define Some JFrame Properties :
        setTitle("Combobox Search Functionality");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setLocation(450,200);
        setSize(700,500);
        setVisible(true);
        setResizable(false);
    }
    //main method start here
    public static void main(String[] args) {
        new comboboxItemSearch();
    }
}

Output:

Java Swing Create Search Funcationality for finding JCombobox items with their current index


Post a Comment

0 Comments