Reputation: 20200
I have some code like so that I was hoping each text line to be centered (instead it was left justified). (I just downloaded latest upgrade of codenameone and intellij yesterday and am starting to play with codenameone). I meant to a long time ago and got distracted.
super("MyApp", BoxLayout.y());
add(new Label("Welcome to the world"));
add(new Label("of"));
add(new Label("Games"));
Here is a picture from the codenameone simulator...
I tried to tweak the very first label to this
Label label1 = new Label("Welcome to the world");
label1.setTextPosition(Component.CENTER);
add(label1);
but that throws an Exception "Text position can't be set to 4". I tried a Container with BorderLayout after that doing an addComponent(BorderLayout.CENTER, new Label("Welcome")) but that ended up left justified as well.
Any ideas how to fix this to be centered?
EDIT: I found a deprecated label.setAlignment method. That fixes the alignment to be centered. Is this maybe a bug in the latest release?
thanks, Dean
Upvotes: 2
Views: 287
Reputation: 52760
Use the alignment property of styling via designer, CSS or Style
object to one of LEFT
, RIGHT
or CENTER
.
setTextPosition
only works in relation to the icon. So if you want the text on the left and the icon on the right you can set it to RIGHT
or even BOTTOM
to put it under the icon.
There is one important caveat. Alignment only works within the box you're placed in so this is only applicable when the component itself is big enough.
Another common pattern to align anything is using flow layout e.g.: Container centered = FlowLayout.encloseCenter(myCmp);
Upvotes: 1