Hello to all! I have added a jcombobox in a jpanel by dragging it from palette (Netbeans IDE). I am populating jcombobox with database without any problem. For example i have following 5 items in my jcombobox:
Italy, Uganda, America, Canada, Austria

When my jtable is focused and i type any key from key board e.g 'A', then the jcombobox filters perfectly and shows me the items: America, Austria. This is perfect working of jcombobox filtering i-e it filters the items on the base of first characters or alphabets from the left.

But I need some advance filtering for example: When i press alphabet key 'U' from the key board, then jcombobox should show me Uganda and Austria

Or when i press alphabet key 'i' from key board, then jcombobox should show me Italy, America, Austria

Or when i type substring 'er' then jcombobox should show me America

I want that my jcombobox should be filtered on any alphabet or substring which are included in items of jcombobox.

For this i have searched for internet and used sample codes and most of them uses Autocompletion of jcombobox, but all in vain. I couldn't get the solution of my problem. Is there any way or property of jcombobox so that i can get my required output?

Thanks in advance

Recommended Answers

All 6 Replies

i type any key from key board e.g 'A', then the jcombobox filters perfectly and shows me the items: America, Austria.

???
When you type a letter the selection in the combo box skips to the first entry that starts with that letter. It doesn't change the contents of the combo box's model.
Presumambly you have some extra code to achieve the filtering behaviour you describe, so maybe you would like to share that with us so we can see what's going on?

Following is the basic code for jcomboBox for its default behaviour and it is working all right as i have discussed above(i-e filters jcomboBox on the base of intital characters or substring from the left).

 jComboBoxItem.setBackground(Color.WHITE);
 ArrayList<String> alItems = new ArrayList<>();
  alItems = obj.selectAllItemNames();   // populating the arrayList alItems from database
  jComboBoxItem.setModel(new DefaultComboBoxModel(alItems.toArray()));

I have no extra code to get my desired behaviour. i want that extra code or technique so that i can get my desired behaviour.

Sorry, but I really don't understand.
When you type a letter into a (not editable) JComboBox the selection jumps to the first item that starts with that letter. If you type the same letter more than once it cycles through all the entries that start with that letter.
If you are seeing the contents filtered when you type then there is some extra code that is doing that.

Here's a tiny stand-alone runnable program that demonstrates standard JComboBox behaviour... one-letter selection, no filtering.

import javax.swing.*;

public class Saboor {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Saboor");
        frame.setMinimumSize(new java.awt.Dimension(500, 200));

        String[] values = {"Italy", "Uganda", "America", "Canada", "Austria"};
        JComboBox jComboBoxItem = new JComboBox(values);
        frame.add(jComboBoxItem);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

Can you produce a tiny stand-alone runnable program that demonstrates the filtering behaviour you are describing?

Brother the small runnable program you provided is the by default behavior of Jcombobox, i-e On typing the keys or string "It" , jcombobox selects "italy"and that is alright and i am implementing it perfectly.
In the defualt working as in your provided program, JCombobox only selects "Italy" when i type 'I' or "It" or "Ita" or "Ital".

But I want that when i type "ta", in this case jcombobox should also select the country name "Italy" as Italy contains "ta". Or when i type "ly" , then jcombobox should also select the "Italy.

In short, When i type any substring of any Country name, jcombobox should select such country name which contains that substring.

I want such working. I hope u understand what I want.

OK. I think you have misunderstood how combo boxes work. When you type "it" what happens is
you type "i" - it selects the first entry starting with i
you type "t" - there is no entry starting with t, so the selection does not change. The fact that t is the second letter of Italy is just a coincidence.

Try adding "Tunisia" to the list of countries. Now when you type "it" it selects Italy, then Tunisia. Selection is based solely on the latest single character that you type.

It's easy enough to change the behaviour to cycle through al the entries that contain the typed char anywhere, but it's a fundamental change to make it responsive to muti-character strings.

Maybe you need to look at a different component - Eg I would think about a text field plus a multi-selection list box. As the content of the text field is changed you can select all matching entries in the list box

When i type any substring of any Country name, jcombobox should select such country name which contains that substring.

If the substring matches more than one country then what do you want to happen? At first it can select the first matching country, but how do you get the second? And how can the user see what they have typed? These are the kind of reasons why I think a combo box is not the right starting point for this.

Try this for a behaviour (ignore code quality - it's just a demo hack):

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Saboor {

    static String[] values = {"Spain", "Italy", "Uganda", "America", "Canada", "Austria", "Tunisia"};

    static JTextField text;
    static JList list;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Saboor");
        frame.setMinimumSize(new java.awt.Dimension(500, 200));

        text = new JTextField(10);
        text.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                select(text.getText());
            }
            public void removeUpdate(DocumentEvent e) {
                select(text.getText());
            }
            public void insertUpdate(DocumentEvent e) {
                select(text.getText());
            }
        });
        frame.add(text, java.awt.BorderLayout.NORTH);

        list = new JList(values);
        list.setVisibleRowCount​(values.length);
        frame.add(list);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

static void select(String input) {
    list.clearSelection();
    for (int i = 0; i < values.length; i++) {
        if (values[i].toUpperCase().contains(input.toUpperCase())) {
            list.addSelectionInterval(i, i);
        }
    }
}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.