Solved

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

Posted on 2013-06-24
2
924 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 35

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 47
mysql jsp example issue 32 74
junit as external jar or library 7 39
JDBC performance 8 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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