
Hello Everyone!!!!!!!! I have a jcombo box with certain values and i want to use it as an editable that means when user will type something in combobox, according to that text only matching item should appear in the list.

Here is an example of Editable JCombobox.
import java.sql.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class EditableComboBox{
public static void main(String[] args) throws Exception{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab=new JLabel("Select");
final JComboBox combo=new JComboBox();
combo.addItem("Nano");
combo.addItem("Alto");
combo.addItem("Swift");
combo.addItem("WagonR");
combo.addItem("Scorpio");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
String name=selectedString(is);
System.out.println("Selected: " + name);
}
};
combo.addActionListener(actionListener);
combo.setEditable(true);
lab.setBounds(20,20,100,20);
combo.setBounds(120,20,80,20);
f.add(lab);
f.add(combo);
f.setVisible(true);
f.setSize(300,120);
}
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
}
For more information, visit the following link:
http://www.roseindia.net/java/example/java/swing/AddRemoveItemFromCombo.shtml
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.