user3229864
user3229864

Reputation: 21

SWT editable Combo - highlight text

I have an SWT Combo that is editable. Users can select an item from the dropdown list or enter new text. My code also does some data validation and if the entered text is not valid, I'd like to force focus to the Combo as well as highlight the entered text.

My question is: is there a way to highlight all the entered text in the Combo? Unfortunately, there is no "selectText" method I think...

Thanks in advance.

I tried Baz's suggestion but for some unknown reasons the code while it works in a separate test project, the same code does not work in my program:

    comboViewerRes = new ComboViewer(this, SWT.DROP_DOWN);
    comboRes = comboViewerRes.getCombo();
    comboRes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
            1, 1));

    comboRes.addListener(SWT.KeyUp, new Listener() {
        @Override
        public void handleEvent(Event e) {
            if (e.keyCode == SWT.SPACE) {
                Rectangle bounds = comboRes.getBounds();
                Point point = new Point(bounds.x, bounds.x + bounds.width);
                System.out.println(point);
                comboRes.setSelection(point);
            }
        }
    });

Upvotes: 2

Views: 3332

Answers (2)

RobEge
RobEge

Reputation: 11

Actually, what I found out is that the combo.setSelection() should take a point with the starting index of the text string as first argument, and second argument as the ending index, i.e. to select the first two characters in the text do, combo.setSelection(new Point(0,2)); and to select all the text do combo.setSelection(new Point(0, combo.getText().length()));

Why it worked for Baz and not user3229864, could be the fact that Baz's combo is on coordinate 0, and user3229864's isn't.

Hope this helps!

Upvotes: 1

Baz
Baz

Reputation: 36894

Ok, there is no intuitive way to do this, however, Combo contains a method called setSelection(Point). The javadoc of this method states:

Sets the selection in the receiver's text field to the range specified by the argument whose x coordinate is the start of the selection and whose y coordinate is the end of the selection.

In order to select everything, you need to do is create a Point, set it's x value to the left edge of the Combo and the y coordinate to the right edge of the Combo.

Here is a code example:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");

    final ComboViewer viewer = new ComboViewer(shell, SWT.DROP_DOWN);

    viewer.getCombo().addListener(SWT.KeyUp, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            if(e.keyCode == SWT.SPACE)
            {
                Rectangle bounds = viewer.getCombo().getBounds();
                Point point = new Point(bounds.x, bounds.x + bounds.width);
                viewer.getCombo().setSelection(point);
            }
        }
    });

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setLabelProvider(new LabelProvider()
    {
        @Override
        public String getText(Object element)
        {
            if (element instanceof String)
            {
                return element.toString();
            }
            return super.getText(element);
        }
    });

    String[] persons = new String[] { "this", "is", "a", "test" };

    viewer.setInput(persons);

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

You can type normally. Once you press space, the code will select everything entered so far.

Here are some screenshots:

enter image description here enter image description here

Upvotes: 2

Related Questions