java - How to set my request focus event listener thread safe? -




i m using the focus event listener implement placeholder solution jtextcomponents (jtextfield, jtextarea ...) implementation below

public class placeholderdecorator {  public static void decorate(jtextfield field, string placeholdingtext) {     field.setforeground(color.gray);     field.addfocuslistener(new focuslistener() {         @override         public void focusgained(focusevent e) {             if (field.gettext().equals(placeholdingtext)) {                 field.settext("");                 field.setforeground(color.black);             }         }          @override         public void focuslost(focusevent e) {             if (field.gettext().isempty()) {                 field.setforeground(color.gray);                 field.settext(placeholdingtext);             }         }     }); } public static void decorate(jtextfield field, string placeholdingtext,color placeholdercolor,color textcolor) {     field.setforeground(placeholdercolor);     field.addfocuslistener(new focuslistener() {         @override         public void focusgained(focusevent e) {             if (field.gettext().equals(placeholdingtext)) {                 field.settext("");                 field.setforeground(textcolor);             }         }          @override         public void focuslost(focusevent e) {             if (field.gettext().isempty()) {                 field.setforeground(placeholdercolor);                 field.settext(placeholdingtext);             }         }     }); }  final public static int date_field_p = 10; final public static int phone_field_p = 14;  public static void decorate(jformattedtextfield field,string placeholdingtext,color placeholdercolor,color textcolor,int checkfieldproperty){     field.setforeground(placeholdercolor);     final jformattedtextfield.abstractformatterfactory formatterfactory = field.getformatterfactory();     field.setformatterfactory(null);     field.settext(placeholdingtext);     field.addfocuslistener(new focuslistener() {         @override         public void focusgained(focusevent e) {             if (field.gettext().equals(placeholdingtext)) {                 field.settext("");                 field.setforeground(textcolor);                 field.setformatterfactory(formatterfactory);             }         }          @override         public void focuslost(focusevent e) {             if (field.gettext().trim().length()!=checkfieldproperty ) {                 field.setformatterfactory(null);                 field.settext("");             }             if (field.gettext().isempty()) {                 field.setformatterfactory(null);                 field.setforeground(placeholdercolor);                 field.settext(placeholdingtext);             }         }     });  } } 

to use place holder tool following

    final color writingcolor = new color(45, 45, 45);     final color holdingcolor = new color(127, 127, 127);      placeholderdecorator.decorate(jftfdatedepot, "date dépot du dossier", holdingcolor, writingcolor, placeholderdecorator.date_field_p);     placeholderdecorator.decorate(jtfnom, "nom", holdingcolor, writingcolor); 

this way when focus occurs place holder shown, after needed creat validation mechanisme inputs got idea check field :

    public class requiredfieldvalidator {          final private list<jtextcomponent> components;         private errordialog dialog;          public requiredfieldvalidator() {             components = new arraylist<>();         }          public void add(jtextcomponent jtextcomponent) {             components.add(jtextcomponent);         }          public boolean validate() {              (final jtextcomponent component : components) {                 string placeholder = component.gettext();                 system.out.println("placeholder : " + placeholder);                  component.requestfocus();                 if (component.gettext().trim().isempty()) {                     system.out.println("validation started");                     dialog = new errordialog(null, true);                     dialog.showerrordialog("veuillez remplir le champs " + placeholder + " obligatoir");                     component.requestfocus();                     return false;                 }              }             return true;         }     } 

as see in code rid of place holding text request focus, text empty if user haven't put text if not input of user remain

i use field validator this

i register required fields in init section

    fieldvalidator = new requiredfieldvalidator();      fieldvalidator.add(jftfdatedepot);     fieldvalidator.add(jftfdatenaissance);     fieldvalidator.add(jftfnumtel);     fieldvalidator.add(jtflieunaissance);     fieldvalidator.add(jtfnom);     fieldvalidator.add(jtfprenom); 

then fire validation process on create event.

my issue in code before running expecting like sequence diagram on create event

getting current component >> requestfocus() >> fire place holding event >> processing on text of component >> return validation >> processed text >> if empty show alert >> if not loop next component

but surprised see request focus listener on current component firing later on swing thread dispatcher

please how make thread safe, need fire focus listener before text of validation.

you should not need request focus on component in order check if component has text or not. user not appreciate when focus keeps moving last text field cycle through text fields validation.

so using current approach, need way see if text in text field "place holder" value or not. maybe can combine "place horder" logic validation logic both methods have access place holder value.

another approach not set text of text field place holder value. validation logic can check if text field empty or not. solution using approach check out text prompt. display prompt without setting text of text field.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -