Label setText() cuts off as text gets longer

34 views
Skip to first unread message

vasilenk...@gmail.com

unread,
Mar 24, 2017, 1:12:54 PM3/24/17
to CodenameOne Discussions
I am working on a school project, in it we are making a game. My issue is with Labels inside an Observer in a Observer/Observable pair. The Observer, called ScoreView, builds the Labels in its Constructor. And a function called update modifies the values whenever they change inside the Observable.
// Inside the constructor. PADDING = 5, increasing or decreasing does not help
// ...
damage = new Label("Player Damage Level:");
damageVal = new Label();
damageVal.getAllStyles().setPadding(Form.LEFT, PADDING);
damageVal.getAllStyles().setPadding(Form.RIGHT, PADDING);
// ...
north.add(damage);
north.add(damageVal);

// inside the update file
damageVal.setText(Integer.toString(gw.getDamageLevel()));

Initially the value is 0, and the damage goes up in increments of 20. So I end up seeing "Player Damage Level:    0",  "....:   2", ".....:  4". When it should be 20, 40, 60, etc. Values that go down, say the fuel level going from  100 and lower, is all fine. As the text gets longer it gets cut off. How do I fix that.

Thank you 

Steve Hannah

unread,
Mar 24, 2017, 6:02:20 PM3/24/17
to codenameone...@googlegroups.com
Calling setText() will cause a repaint of the Label, but the bounds of the label will be unchanged because they are set by the parent container's layout manager.  Call damageVal.getParent().revalidate() to cause the parent to be re-laid out.  (If the parent container also doesn't have "room" for the larger label, you may need to revalidate further up the hierarchy.

The catch all is
damageVal.getComponentForm().revalidate();

which will relayout the whole form.

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/e95227d4-2409-45c8-9c53-43f24bf7d6e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One
Message has been deleted

vasilenk...@gmail.com

unread,
Mar 24, 2017, 10:16:57 PM3/24/17
to CodenameOne Discussions
That worked, thank you!
Reply all
Reply to author
Forward
0 new messages