Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1162
  • Last Modified:

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

0
Tolgar
Asked:
Tolgar
2 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You must make it so the the "txt" object that you are creating in the code above is accessible from the code that runs when you press the button. One way is to make it an instance variable of the enclosing class, assuming that the above code and the buttons code is in the same class. Once you do that, you can then just call
txt.setText("");

Open in new window

0
 
ValeriCommented:
1. add "clearItem" to your ToolBar menu
2. add clearItem.addListener(SWT.Selection, toolBarListener); as well
3. add this to ToolBarListener class:
} else if (toolItem == Gui.clearItem) {
  Gui.clearTxt();
}
4. add this function to your Gui class
public static void clearTxt() {
  txt.setText("");
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now