5

I want to scroll Horizontally in label Field.

In Below image, Long Text which is E-mail ID is not scrolled.

I am adding this LabelField in Custom GridField Manager. Here is the code of Custom GridField Manager.

public class CustomGridFieldManager extends Manager {
private int[] columnWidths;
private int columns;
private int allRowHeight = -1;


public CustomGridFieldManager(int columns, long style) {
    super(style);
    this.columns = columns;
}


public CustomGridFieldManager(int[] columnWidths, long style) {
    super(style);
    this.columnWidths = columnWidths;
    this.columns = columnWidths.length;

}

public CustomGridFieldManager(int[] columnWidths, int rowHeight, long style) {
    this(columnWidths, style);
    this.allRowHeight  = rowHeight;
}

protected boolean navigationMovement(int dx, int dy, int status, int time) {

    int focusIndex = getFieldWithFocusIndex();
    while(dy > 0) {
        focusIndex += columns;
        if (focusIndex >= getFieldCount()) {
            return false; // Focus moves out of this manager
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) { // Only move the focus onto focusable fields
                f.setFocus();
                dy--;
            }
        }
    }
    while(dy < 0) {
        focusIndex -= columns;
        if (focusIndex < 0) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dy++;
            }
        }
    }

    while(dx > 0) {
        focusIndex ++;
        if (focusIndex >= getFieldCount()) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dx--;
            }
        }
    }
    while(dx < 0) {
        focusIndex --;
        if (focusIndex < 0) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dx++;
            }
        }
    }
    return true;
}

protected void sublayout(int width, int height) {
    int y = 0;
    if (columnWidths == null) {
        columnWidths = new int[columns];
        for(int i = 0; i < columns; i++) {
            columnWidths[i] = width/columns;
        }
    }
    Field[] fields = new Field[columnWidths.length];
    int currentColumn = 0;
    int rowHeight = 0;
    for(int i = 0; i < getFieldCount(); i++) {
        fields[currentColumn] = getField(i);
        layoutChild(fields[currentColumn], columnWidths[currentColumn], height-y);
        if (fields[currentColumn].getHeight() > rowHeight) {
            rowHeight = fields[currentColumn].getHeight();
        }
        currentColumn++;
        if (currentColumn == columnWidths.length || i == getFieldCount()-1) {
            int x = 0;
            if (this.allRowHeight >= 0) {
                rowHeight = this.allRowHeight;
            }
            for(int c = 0; c < currentColumn; c++) {
                long fieldStyle = fields[c].getStyle();
                int fieldXOffset = 0;
                long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK;
                if (fieldHalign == Field.FIELD_RIGHT) {
                    fieldXOffset = columnWidths[c] - fields[c].getWidth();
                }
                else if (fieldHalign == Field.FIELD_HCENTER) {
                    fieldXOffset = (columnWidths[c]-fields[c].getWidth())/2;
                }

                int fieldYOffset = 0;
                long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;
                if (fieldValign == Field.FIELD_BOTTOM) {
                    fieldYOffset = rowHeight - fields[c].getHeight();
                }
                else if (fieldValign == Field.FIELD_VCENTER) {
                    fieldYOffset = (rowHeight-fields[c].getHeight())/2;
                }

                setPositionChild(fields[c], x+fieldXOffset, y + fieldYOffset);
                x += columnWidths[c];
            }
            currentColumn = 0;
            y += rowHeight;
        }
        if (y >= height) {
            break;
        }
    }
    int totalWidth = 0;
    for(int i = 0; i < columnWidths.length; i++) {
        totalWidth += columnWidths[i];
    }
    setExtent(totalWidth, Math.min(y, height));
}

}

In another Class, I use this custom GridField Manager Class.

int[] width = { (int) (Display.getWidth() / 2.9),
                (int) (Display.getWidth() / 1.1) };

      final CustomGridFieldManager gfm_transactioninfo = new CustomGridFieldManager(
                width, 35, Manager.VERTICAL_SCROLL | Manager.FIELD_HCENTER
                        | FOCUSABLE) {
            protected void paint(Graphics graphics) {
                // TODO Auto-generated method stub
                graphics.setColor(AppData.color_black);
                super.paint(graphics);
            }

        };
        gfm_transactioninfo.setMargin(10, 0, 0, 10);// set top and left margin

I add Labelfiled like this,

lbl_CustEmail = new LabelField("Customer Email", LabelField.FOCUSABLE);
        lbl_CustEmail.setFont(label_font);

        value_CustEmail = new LabelField(": " +trandtail[0].getFromEmail());
        value_CustEmail.setFont(label_font);

   gfm_transactioninfo.add(lbl_CustEmail);
   gfm_transactioninfo.add(value_CustEmail);

If any one has any idea regarding How to scroll Horizontally then please help me. Thanks in Advance.

Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
Riddhi Barbhaya
  • 1,205
  • 1
  • 11
  • 19
  • try http://stackoverflow.com/questions/2613062/how-can-i-display-scroll-text-like-marque-in-blackberry-using-j2me – Rince Thomas Mar 29 '14 at 13:14
  • How do you expect trackpad users to be able to scroll, when you override navigationMovement? – Peter Strange Mar 30 '14 at 17:15
  • Is there a specific reason you cannot simply put the label within a FieldManager set to horizontally scrollable? ie `gfm_transactioninfo.add(manager);` – Kevin Mar 31 '14 at 09:32
  • @Kevin - one problem with that approach is that he has overridden navigationMovement in the GFM, so how is he ever going to scroll a Field within that Manager on a non touchscreen device? – Peter Strange Apr 01 '14 at 20:40

1 Answers1

2

By customizing your grid view ,you may add one FocusableNullField before and after the label field. By doing so once the focus is on the first null field you can scroll horizontally to the next focusablenullfield and explicitly make labelfield scrollable.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • 1
    Don't think this will work, I think adding NullFields just means you can focus on the start and the end, not scrollable. You can make the LabelField focusable to achieve a similar effect to the NullField at the start, or even replace the LabelField with a RichTextField which maintains a 'caret' and so is scrollable in that regard. There are a number of similar options, but the issue for me is that none of them will work on a trackpad device with the navigationMeovement override as supplied. Hence my question which the OP has not answered. – Peter Strange Apr 02 '14 at 14:45