Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to delete all characters in a text box using SWT?

Hi,
I have GUI that I create using SWT. I have a text box in this GUI where I can type some characters.

I have a button in the toolbar that is supposed to delete everything in this text box.

This is how I create this text box. I have a TxtFocusListener.java class as shown below:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class TxtFocusListener implements FocusListener {
    
    static final String introText = "Use the buttons above to select files/directories";
    private Composite parent;

    public TxtFocusListener(Composite parent) {
        this.parent = parent;
    }

    @Override
    public void focusGained(FocusEvent e) {
        Text t = (Text)e.widget;
        if (t.getText().equalsIgnoreCase(introText)) {
            t.setText("");
            t.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        Text t = (Text)e.widget;
        if (t.getText().equalsIgnoreCase("")) {
            t.setText(introText);
            t.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        }
    }

}

Open in new window


And this is how I create the text box:
  GridLayout groupLayout = new GridLayout();
        GridData groupGD = new GridData(GridData.FILL_BOTH);
        Group groupText = new Group(compTab1, SWT.NONE);
        groupText.setLayout(groupLayout);
        groupText.setLayoutData(groupGD);
        groupText.setText("Files in List");
       
        GridData txtGridData = new GridData(GridData.FILL_BOTH);
        Text txt = new Text(groupText, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        TxtFocusListener txtFL = new TxtFocusListener(compTab1);
        txt.setLayoutData(txtGridData);
        txt.addFocusListener(txtFL);
        txt.setText(TxtFocusListener.introText); txt.setForeground(compTab1.getDisplay().getSystemColor(SWT.COLOR_GRAY));

Open in new window

SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial