0

I am trying to set some LabelField text during its layout (inside the layout() method) depending on display's orientation in the following way:

LabelField roomDescLabel = new LabelField("", Field.FIELD_VCENTER) {
    protected void layout(int width, int height) {
        if (Display.ORIENTATION_LANDSCAPE == orientation) {
            setText("LANDSCAPE");
        } else {
            setText("NOT LANDSCAPE");
        }
        super.layout(width, height);
    }
};

Sometimes, I am catching an IllegalStateException with Layout requested during layout message. The message makes sense since that's exactly what I am doing (I am calling setText() which in its turn requests layout update). Is there any acceptable way to set the text value depending on display's orientation?

I've found a solution to this issue but I am not totally satisfied about it. The solution would be to override the LabelField manager's sublayout() method and replace the existing label field with a new one with updated text (and only than call to super.sublayout()). Surprisingly, this workaround works as expected and is not triggering layout update.

UPDATE: Another possible solution would be to call invokeLater() with the setText() operation in the layout() method of the LabelField. The disadvantage in this approach compared to the solution I proposed earlier is that sublayout() and paint() methods will be called twice.

tonymontana
  • 5,728
  • 4
  • 34
  • 53
  • 1
    I think overriding the `paint` method can be helpful. On the `sublayout` it is possible to save the label text to draw on some temporary variable and calculate the extent of the `LabelField` depending on screen orientation. And on `paint` we can draw that label text saved on temporary variable. – Rupak Jun 05 '12 at 17:14
  • @Rupak Good point. Can be easily done for `LabelField`. Can be more complex for other fields - say `RichTextField`, where the text can't be drawn with just one straight `Graphics#drawText()` call. – tonymontana Jun 05 '12 at 17:24
  • @Rupak Actually, even in case of `LabelField`, calculating the extent can be very complex. Consider the case where `LabelField` text spans over multiple lines. One will have to implement a *wrapping* mechanism logic. – tonymontana Jun 05 '12 at 20:58
  • Yes, it will be complex to support different alignment and drawing style. – Rupak Jun 06 '12 at 03:37

0 Answers0