Using a AutoCompleteTextField from GUI builder

97 views
Skip to first unread message

Gareth Murfin

unread,
Oct 23, 2013, 7:54:34 AM10/23/13
to codenameone...@googlegroups.com
Is it possible to use an AutoCompleteTextField  from the GUI builder? If not how feasible is it in code to swap out my existing TextFields with them without anyone noticing and without losing any restraints and things Ive applied to them ? I read this


and definitely want to use them but I already have all my forms built and working wih normal textfield!.. 

Thanks

Gareth Murfin

unread,
Oct 23, 2013, 7:59:55 AM10/23/13
to codenameone...@googlegroups.com
Also if you have a dynamic list of predictions wont you have to keep remaking the component to pass it a new list model? or is there a preffered way to do this without remaking each time. ?

Gareth Murfin

unread,
Oct 23, 2013, 8:14:54 AM10/23/13
to codenameone...@googlegroups.com
I tried replacing one for another like this:

 TextField startpointtf = (TextField) findByName("startpointtf", f);
        
        //swap out real one with auto complete one
        AutoCompleteTextField startAC = new AutoCompleteTextField(new String[] {"Common", "Code", "Codename One", "Correct", "Correlation", "Co-location", "Corporate"} );
        startAC.setPreferredSize(startpointtf.getPreferredSize());
        startAC.setText("TEST");
        f.replace(startpointtf,startAC, CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));

unfortunately the new textfield appeared in a completely different place above the old one, and then touching either of them gives a null pointer,

java.lang.NullPointerException
at com.codename1.ui.TextField.initComponentImpl(TextField.java:1493)
at com.codename1.ui.Container.initComponentImpl(Container.java:675)
at com.codename1.ui.Container.initComponentImpl(Container.java:675)
at com.codename1.ui.Container.initComponentImpl(Container.java:675)
at com.codename1.ui.Container.insertComponentAt(Container.java:443)
at com.codename1.ui.Container.addComponent(Container.java:388)
at com.codename1.ui.Form.getLayeredPane(Form.java:722)
at com.codename1.ui.AutoCompleteTextField$FormPointerListener.actionPerformed(AutoCompleteTextField.java:181)

Chen Fishbein

unread,
Oct 23, 2013, 8:25:01 AM10/23/13
to codenameone...@googlegroups.com
replace works only on the direct parent try this instead:

startpointtf.getParent().replace(startpointtf, startAC, ...)

also do this on the before show method of the Form and without transition.

Regarding the dynamic list, there is a set model on the AutoCompleteTextField which you can use to give your own list model that brings data dynamically from somewhere.
 

Gareth Murfin

unread,
Oct 23, 2013, 8:27:15 AM10/23/13
to codenameone...@googlegroups.com
thanks very much for the excellent help, although I do not see any way to set the model?

Chen Fishbein

unread,
Oct 23, 2013, 8:28:37 AM10/23/13
to codenameone...@googlegroups.com
one of the constructors accepts a ListModel

Gareth Murfin

unread,
Oct 23, 2013, 8:31:46 AM10/23/13
to codenameone...@googlegroups.com
Yeh but then I have to reconstruct the component every time to change this model? I want to supply a new model every key press or so

Gareth Murfin

unread,
Oct 23, 2013, 9:23:19 AM10/23/13
to codenameone...@googlegroups.com
OK I appear to have an implementation working, I took advice form the cn1 blog about overriding the AutoCompleteTextField etc, and it seems to work - BUT... my popup ONLY appears when I select off the textfield, if it is focused my popup list does not show! Any ideas?

Im well excited about this once it works I will upload a video hehe..

Gareth Murfin

unread,
Oct 23, 2013, 9:31:45 AM10/23/13
to codenameone...@googlegroups.com
If I replace in this way it seems to just dissapear.

Gareth Murfin

unread,
Oct 23, 2013, 9:36:45 AM10/23/13
to codenameone...@googlegroups.com
Does it matter that its in a grid layer?

Vanishes even when i do this:

  //SWAP out the ordinart textfields for auto completes, we need to do this in before and post methods
         TextField startpointtf = (TextField) findByName("startpointtf", f);
        
        //swap out real one with auto complete one
        AutoCompleteTextField startAC = new AutoCompleteTextField(new String[] {"Common", "Code", "Codename One", "Correct", "Correlation", "Co-location", "Corporate"} );
       // startAC.setPreferredSize(startpointtf.getPreferredSize());
        startAC.setConstraint(startpointtf.getConstraint());
        startAC.setText("TEST");
        startAC.setUIID(startpointtf.getUIID());
        
        startpointtf.getParent().replace(startpointtf,startAC, null);// CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
        

Chen Fishbein

unread,
Oct 23, 2013, 9:51:48 AM10/23/13
to codenameone...@googlegroups.com
not sure what you are experiencing the layout shouldn't change in this case.
try to debug this and see what happens on the FormPointerListener inner class that suppose to manage the show/hide of the popup according to the pointer events on the Form

Gareth Murfin

unread,
Oct 23, 2013, 10:16:34 AM10/23/13
to codenameone...@googlegroups.com
OK thanks mate, will check it out and report back, for now here is my autocomplete textfield doing some cool stuff-- almost :-)

Chen Fishbein

unread,
Oct 23, 2013, 11:18:46 AM10/23/13
to codenameone...@googlegroups.com
that's cool!, how are you adding/changing items in the model? it seems like the List does not get notified once you change/replace items.
Reply all
Reply to author
Forward
0 new messages