2

I'd like to add an icon inside a TextField in Codename One, to get a simulare result to what is described in this other SO Question.

I could not find an example in the API JavaDoc. Should I define a new component Style (UIID) for each Textfield with a different icon as background (although I am not sure it will yield the expected result) ? Or is there a smarter way to do it ?

Any help appreciated,

Community
  • 1
  • 1
HelloWorld
  • 2,275
  • 18
  • 29

1 Answers1

2

There are two different ways that will produce two different results. The text field hint can accept an icon so something like:

tf.getHintLabel().setIcon(myIcon);

Will work when there is no text in the field and vanish when there is.

If you want the icon to stay you can just use something like this:

Container cnt = BorderLayout.centerEastWest(myTextField, new Label(icon), null);
cnt.setUIID("TextField");
textField.getAllStyles().setBorder(Border.createEmpty());
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks Shai! I found the first way much simpler since my TextField had rounded border and the Form background did not have the same color as the TextField one. By the way I used the "direct" method `setHintIcon()` like tf.setHintIcon(myIcon); – HelloWorld May 10 '17 at 07:43
  • Forgot about `setHintIcon` ;-) – Shai Almog May 11 '17 at 05:51