Reputation: 13
I'm trying to set up a default value to in the comboBox, since comboBox also supports searching the default value is taken as search string by default and i need to perform 2 action i.e, to clear the default value and the the displaying the other entries in the list. So how can i clear the default text upon clicking the dropDown button so that all my list values are visible.
Upvotes: 0
Views: 1357
Reputation: 21
From the description of the question I assume that you wanted to have a placeholder text on the Combo which is just a hint to be displayed by default always and once the user clicks on the dropdown it should clear the place holder text from the combo.
If this is your requirement then you can make first set the Text on the combo using setText("some text") and then use focus listener to clear the Text. Below is the code snippet.
Combo userCombo= new Combo(shell, SWT.DROP_DOWN);
String[] users= new String[] { "User1", "User2", "User3" };
userCombo.setItems(users);
userCombo.setText("select user from the dropdown");
userCombo.addFocusListener(new FocusListener() {
@override
public void focusLost(FocusEvent arg0){
if(userCombo.getSelectionIndex() == -1){
userCombo.setText("select user from the dropdown");
}
}
@override
public void focusGained(FocusEvent arg0){
if(userCombo.getSelectionIndex() == -1){
userCombo.setText("");
}
}
});
Also if you want to clear the selection upon click of dropdown then you can add Mouse Drop Down Event to combo and clear the selection using the following calls.
combo.deselectAll() to remove the selections
combo.clearSelection()
Sets the selection in the receiver's text field to an empty selection starting just before the first character. If the text field is editable, this has the effect of placing the i-beam at the start of the text. Note: To clear the selected items in the receiver's list, use deselectAll()."
Upvotes: 0
Reputation: 2038
You can make use of SWT mouseDown event for this. Refer Mouse Adapter options
Below is the sample code where it will clear the selection when you click on drop down button.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class ComboMainClass
{
public static void main(final String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
RowLayout rowLayout = new RowLayout();
rowLayout.marginLeft = 10;
rowLayout.marginTop = 10;
shell.setLayout(rowLayout);
Label label = new Label(shell, SWT.NONE);
label.setText("Select Items:");
Combo combo = new Combo(shell, SWT.DROP_DOWN);
String[] items = new String[] { "Item One", "Item two", "Item three" };
combo.setItems(items);
combo.addMouseListener(new MouseAdapter()
{
@Override
public void mouseDown(final MouseEvent e)
{
combo.setText("");
}
});
shell.setText("SWT Combo");
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}
Upvotes: 1