Solved

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

Posted on 2013-06-24
2
981 Views
Last Modified: 2013-07-09
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
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 39273595
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
 
LVL 16

Accepted Solution

by:
Valeri earned 250 total points
ID: 39301777
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question