The problem about Focuslistener in SWT

Source: Internet
Author: User
Tags add
Problem in SWT Focuslistener can be used to monitor whether a TextField is selected,
In the following program, I add Focuslistener to two text, which is the function of
When text is selected, the above text will be selected as a full state.
I don't know why it works only for the first one.
It was later discovered that the two-text type was different.
Just the text of Swt.single, as if SelectAll () is invalid.

Textfield1.java
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.FocusEvent;
Import Org.eclipse.swt.events.FocusListener;
Import org.eclipse.swt.widgets.*;

public class TextField1 {

Display D;
Shell s;
TextField1 () {
D = new Display ();
s = new Shell (d);
S.setsize (250,250);
S.settext ("A Text Field Example");
Text Text1 = new text (s, SWT. WRAP | Swt. BORDER);
Text1.setbounds (100,50,100,20);
Text1.settextlimit (5);
Text1.settext ("12345");
Text Text2 = new text (s, SWT. Single | Swt. BORDER);
Text2.setbounds (100,75,100,20);
Text2.settextlimit (10);

Add a Focus listener
Focuslistener Focuslistener = new Focuslistener () {
public void focusgained (FocusEvent e) {
Text T = (text) e.widget;
T.selectall ();
}
public void Focuslost (FocusEvent e) {
Text T = (text) e.widget;
if (T.getselectioncount () > 0) {
T.clearselection ();
}
}
};
Text1.addfocuslistener (Focuslistener);
Text2.addfocuslistener (Focuslistener);
S.open ();
while (!s.isdisposed ()) {
if (!d.readanddispatch ())
D.sleep ();
}
D.dispose ();
}
public static void Main (string[] args) {
TextField1 tx = new TextField1 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.