Solved

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

Posted on 2013-06-24
2
802 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
2 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now