views:

622

answers:

1

Hello everyone I am new to blackberry and I want a textfield that scroll it's text i.e greater than the preferred width horizontally , also able to show label out side the text draw area ( e.g. on the left side). Please help me.

+3  A: 

This may be achieved by combining non-scrolling and scrolling HorizontalFieldManagers.

Try this code:

class Scr extends MainScreen {
    public Scr() {
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        editHolder.add(new TextField(TextField.NO_NEWLINE));
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}

Setting default text code:

class Scr extends MainScreen {
    public Scr() {
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField(TextField.NO_NEWLINE);      
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);

        // set some text then
        String text = "Lorem ipsum dolor sit amet, consectetuer"+
        " adipiscing elit, sed diam nonummy nibh euismod tincidunt"+
        " ut laoreet dolore magna aliquam erat volutpat.";
        textField.setText(text);
    }
}

And something which basically works on 4.6/4.7:

class Scr extends MainScreen {
    public Scr() {
        String text = "Lorem ipsum dolor sit amet, consectetuer"+
        " adipiscing elit, sed diam nonummy nibh euismod tincidunt"+
        " ut laoreet dolore magna aliquam erat volutpat.";
        HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
                NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField("",text,1024,TextField.NO_NEWLINE);
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}

Border for Manager

Border border = BorderFactory.createSimpleBorder(new XYEdges(4,4,4,4));
fieldHolder.setBorder(border);

Fixed size Manager

class SizedHFM extends HorizontalFieldManager {
    int mWidth = 0;

    public SizedHFM(int width) {
        super(NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
        mWidth = width;
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(mWidth, maxHeight);
        setExtent(mWidth, getPreferredHeight());
    }
}

Sample of use:

class Scr extends MainScreen {
    public Scr() {
        String text = "Lorem ipsum dolor sit amet, consectetuer"
                + " adipiscing elit, sed diam nonummy nibh euismod tincidunt"
                + " ut laoreet dolore magna aliquam erat volutpat.";
        SizedHFM fieldHolder = new SizedHFM(200);
        Border border = BorderFactory
                .createSimpleBorder(new XYEdges(4, 4, 4, 4));
        fieldHolder.setBorder(border);
        fieldHolder.add(new LabelField("some label: "));
        HorizontalFieldManager editHolder = new HorizontalFieldManager(
                HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
        TextField textField = new TextField("", text, 1024,
                TextField.NO_NEWLINE);
        editHolder.add(textField);
        fieldHolder.add(editHolder);
        add(fieldHolder);
    }
}
Max Gontar
Thanks Max for your quick reply but here I am not able to find the TextField constructor with four parameters in rim package.
Sachin
Try to use import net.rim.device.api.ui.component.TextField; (for 4.6 and >)
Max Gontar
for 4.5 and < see code update
Max Gontar
even better without useless stuff...
Max Gontar
I have tried with it but TextField constructor is either empty or taking only style. And I am using JDE 5.0.0 and api version is 4.7
Sachin
that is strange. have you tried to compile it regarding what auto-complete says?
Max Gontar
Thank u so much max now it is working properly but what if I want to set it's width and draw a rectangle around it so it will look like textfields in other languages.
Sachin
See update. You really should read all of this: http://stackoverflow.com/questions/1445953/blackberry-user-interface-design-customizable-ui your answer is partially answered in other posts here. I just have occasion to answer it but in the future do some search before asking ;)
Max Gontar
thanks max for your valuable suggestions and help provided by you
Sachin
You're welcome! If this answer was the best one, please accept it (click check mark on the left side near answer)
Max Gontar