1

I have a LabelField nested within a TableLayoutManager row. I want the row to be a specific height (the same height as its bitmap background). In order to achieve this, I changed the layout() method of the nested LabelField:

LabelField lblHours = new LabelField(hours,
            Color.BLACK, Manager.FIELD_VCENTER) {
        protected void layout(int width, int height) {
            super.layout(width, 40 //height of the bitmap);
            setExtent(width, 40 //height of the bitmap);
        }
    };

This successfully increased the TableLayoutManager row size. However, once I do this, the LabelField is no longer centered vertically within the row. Any suggestions on how to do that?

Thanks!

littleK
  • 19,521
  • 30
  • 128
  • 188
  • I think you need to provide more code - how are you adding the LabelField to the TableLayoutManager? – Michael Donohue May 25 '11 at 20:01
  • Hi Michael, I have a switch statement that is essentially going through the days of the week (1-7), and for each case, it will instantiate my custom class that extends TableLayoutManager and add it to the current VerticalFieldManager parent. Is that what you're looking for? Let me know, I can certainly provide specific code... – littleK May 25 '11 at 20:50

2 Answers2

1

The above answer works just make sure that you put in two statements for the above.. as illustrated in the below example.

LabelField importanceLabel = new LabelField("Importance: ",LabelField.FIELD_VCENTER | LabelField.FIELD_RIGHT) {
          public void paint(Graphics g) { 
              g.setColor(0x145085); 
              super.paint(g); 
          } 
          protected void layout(int width, int height) {
              super.layout(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
              setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
          }
      };

This is going to be the same response.. and thank you guys.. for the discussion. It helped me.

Francisco R
  • 4,032
  • 1
  • 22
  • 37
medampudi
  • 399
  • 3
  • 15
0

Try changing setExtent(width, 40) to setExtent(this.getWidth(), 40). This could possibly not work if getWidth() is trying to take up all of the available width (which is what your setExtent() call is doing), and since the text will be drawn left-aligned, it looks like your field isn't centered but in reality it's just taking up the entire cell of your table. Alternatively, if this doesn't work you may be able to do setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 40);

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • Hi jprofitt, Thanks for your help. Unfortunately, both of your suggested solutions are yielding the same result as before... – littleK May 25 '11 at 18:00
  • Was the LabelField centered before you made the change? I was under the impression that it was. – jprofitt May 25 '11 at 19:05
  • Yes, the LabelField was centered vertically before I set a custom height for it. – littleK May 25 '11 at 19:09
  • Maybe rather than overriding layout() try changing getPreferredHeight() to return 40 and let it do its normal layout – jprofitt May 25 '11 at 19:56
  • I've tried that, and it doesn't increase the row height at all, oddly enough. – littleK May 25 '11 at 20:48
  • Just for now, override the paint() method to draw a rectangle at the Field's extent, that way we can be sure how big the Field is. If it is just left aligned then it's a different problem. After that maybe try doing the same thing that I mentioned in my last comment but with getContentWidth() – jprofitt May 25 '11 at 21:49
  • jprofitt, the rectangle is indeed the correct width and the correct height (40 pixels). This shows as being true for everything that we have tried. (Except for using getPreferredHeight(), which does not expand the row to 40)... – littleK May 26 '11 at 17:38