Search code examples
blackberryblackberry-jde

Center ButtonField in a FieldManager



I wanna center a buttonField in a HorizontalFieldManager
i tried that code :

   HorizontalFieldManager ButM = new HorizontalFieldManager(Field.FIELD_HCENTER|USE_ALL_WIDTH)
    {
        public void paint(Graphics graphics)
        {
            graphics.setBackgroundColor(0x00000000);
            graphics.clear();
            super.paint(graphics);
        }  
    };
    ButtonField Order = new ButtonField("Tri",Field.FIELD_HCENTER|ButtonField.CONSUME_CLICK);

But the ButtonField is not centred, am i doing something wrong ?

EDIT :
I solved this problem by using this code :

HorizontalFieldManager ButM = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER|USE_ALL_WIDTH)
    {
      public void paint(Graphics graphics)
      {
          graphics.setBackgroundColor(0x00000000);
          graphics.clear();
          super.paint(graphics);
      }  
    };
    ButtonField Order = new ButtonField("Tri",DrawStyle.HCENTER|ButtonField.CONSUME_CLICK);
    Order.setMargin(0,0,0,(Display.getWidth()/2)-30);

Solution

  • HorizontalFieldManager ButM = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER)
    {
      public void paint(Graphics graphics)
      {
          graphics.setBackgroundColor(0x00000000);
          graphics.clear();
          super.paint(graphics);
      }  
    };
    ButtonField Order = new ButtonField("Tri",DrawStyle.HCENTER|ButtonField.CONSUME_CLICK);
    Order.setMargin(0,0,0,(Display.getWidth() - Order.getPreferredWidth())/2 );
    ButM.add(Order);
    

    This code will run fine.. I have test it...