Search Icon inside a Editable JComboBox / JTextField

1.2k views Asked by At


I have an Editable JComboBox search field in one of my Java swing application. I wanted to save space of the search box header and put in search icon inside the editable jcombobox to depict it as a search field. Which should look quite similar to a Facebook search field.
So, my question is that, Is there any way to achieve such a thing in Java? As a beginner my idea is void about this. Any suggestion / help would positively work for me.

Thanks

1

There are 1 answers

3
camickr On BEST ANSWER

You can use a custom Border on the editor of the combo box which is a text field:

ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
Border outer = textField.getBorder();
Border search = new MatteBorder(0, 16, 0, 0, new ImageIcon("search.gif"));
textField.setBorder( new CompoundBorder(outer, search) );

The MatteBorder will tile the image in the Border, so you need to make sure the image is the size of the text field, or you create a custom Border that only paints the image once.

Read the section from the Swing tutorial on How to Use Borders for more information.