Skip to main content

autocomplete jcombobox

6 replies [Last post]
csergiu77
Offline
Joined: 2005-02-06

Hi there,

I´m looking for a free auto-complete combobox component that behaves
like firefox 3 url bar, I mean, the auto-complete feature must display
items containing a given substring, not only items beginning with this
substring.

For example, if a type "ri" in a combobox of font names, it should
display not only fonts beginning with "ri", but also fonts containing
"ri" substring, like "Arial".

Thanks in advance,

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
csergiu77
Offline
Joined: 2005-02-06

I found a small sample on the net that i adapted to my needs.
So practically this is all that i need in terms of usage.
If you can take a look and running it you'll see hwo it works.
It is just a sample of how i would like to work the swingx component as well.

public class Sample extends JFrame {
JComboBox combo = null;
Vector v = null;
public Sample() {
this.getContentPane().setLayout(null);
this.setLocation(300, 30);
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
v = new Vector();
v.add("");
v.add("Romania");
v.add("Suedia");
v.add("Elvetia");
v.add("Portugalia");
v.add("Islanda");
v.add("Ungaria");
v.add("Ucraina");
v.add("Bulgaria");
v.add("Franta");
combo = new JComboBox();
combo.setEditable(true);
combo.setSelectedItem("");
for (int i = 0; i < v.size(); i++) {
combo.addItem(v.get(i));
}
combo.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() != 38 && e.getKeyCode() != 40 && e.getKeyCode() != 10) {
String a = combo.getEditor().getItem().toString();
combo.removeAllItems();
boolean showPopup = false;

for (int i = 0; i < v.size(); i++) {
if (v.get(i).indexOf(a) != -1) {
combo.addItem(v.get(i));
showPopup = true;
}
}
combo.getEditor().setItem(new String(a));
JTextField textField = (JTextField) e.getSource();
textField.setCaretPosition(textField.getDocument().getLength());
combo.hidePopup();
if (showPopup) {
combo.showPopup();
}
}
}
});

combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (combo.getSelectedIndex() == -1) {
//WHY selection do not work without this line !!!
combo.setSelectedItem(combo.getItemAt(0));
}
}
});

combo.setBounds(20, 20, 150, 20);
this.getContentPane().add(combo);

this.setVisible(true);

}

public static void main(String[] args) {
try {
// Set System L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// handle exception
}
Sample testing;
testing = new Sample();
}}

kschaefe
Offline
Joined: 2006-06-08

Does not exist, but we do have that as a feature request.

Karl

csergiu77
Offline
Joined: 2005-02-06

Since i am very very interested in this....

may i help ?

kschaefe
Offline
Joined: 2006-06-08

Yes, you can help. Here's how.

You can submit a patch to the bug. If it's larger than 20 lines, you have to sign the JCA on the SwingLabs.org site.

If the change is large (more than one file or new classes), it would be best if you joined the project incubator and completed the work there. We can then review the work and bring it over. This will need a signed JCA.

Karl

csergiu77
Offline
Joined: 2005-02-06

Well i take a look into the code and i say that is not going to be resolved in 20 lines of code (maybe i am wrong)

I saw that you put

//TODO entry point for handling 739
private boolean startsWithIgnoreCase(String base, String prefix) {
if (base.length() < prefix.length()) return false;
return base.regionMatches(true, 0, prefix, 0, prefix.length());
}

739 is exaclty my feature request as well.

But i am affraid that this is not the entry poin since in the autocomplet combo i would like to build data in the model is changed based on the text present in editable combo.

In the current case the data from combo model is the same only selection is changed no ?

Thx for any tip

csergiu77
Offline
Joined: 2005-02-06

//WHY selection do not work without this line !!!
combo.setSelectedItem(combo.getItemAt(0));
}
}
});

combo.setBounds(20, 20, 150, 20);
this.getContentPane().add(combo);

this.setVisible(true);

}

public static void main(String[] args) {
try {
// Set System L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// handle exception
}
Sample testing;
testing = new Sample();
}}

Message was edited by: csergiu77