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..
- setText() : Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.
- getText() : returns the text from the single-line text field.
- setFont() : sets the font and style when displaying text strings.
- setBounds(): set the position and size of any component.
- add() : add a component on another component or JFrame.
- setLocation(): Sets the location of the point to the specified location.
- setLayout() : sets the layout manager for the component.
- 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.
- setVisible() : sets the visibility of the component or JFrame. It is by default false.
- addKeyListener Iterface and keyReleased method : Generated when input is received from keyword.
- 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.
- getItemCount() method : Returns the number of items in the combobox.
- setSelectedIndex(int anindex) method : Selects the item at index anIndex.
- 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();
}
}
0 Comments
Enter Your Comment