Reputation: 1141
I would like to highlight a text in a combo box (org.eclipse.swt.widgets.Combo
).
For example, suppose that the combo text is "IP:6061". I would like to highlight "IP". How can I do that?
Upvotes: 0
Views: 893
Reputation: 36884
Here's a reusable solution:
private void setSelection(Combo combo, String query)
{
String comboText = combo.getText();
int index = comboText.indexOf(query);
if(index != -1)
combo.setSelection(new Point(index, index + query.length()));
}
You could add an else
clause to remove the selection if that's what you want to happen when there's no match in the text.
Call it like this:
Combo combo = ...
combo.setText("IP: 1.1.1.1");
String query = "IP";
setSelection(combo, query);
Upvotes: 3
Reputation: 899
You may have to use a combination of getSelection()
and getText()
. But this question is very vague; so you maybe asking only to need to use getText()
and filter the string to the characters you want.
Upvotes: 0
Reputation: 20985
To select a part of the Combo's text, use Combo::setSelection()
For example
combo.setText( "IP:6061" );
combo.setSelection( new Point( 0, 2 ) );
would select the 'IP' of 'IP:6061'.
Upvotes: 2