varun
varun

Reputation: 41

Blackberry - Custom EditField Cursor

I am creating a search box for my BlackBerry project, but it looks like BlackBerry doesn't have an API for creating a single line EditField. I have created a custom field by extending BasicEditField and overriding methods like layout and paint. In the paint function body, I am drawing a rectangle with getPreferredWidth() and getPreferredHeight(). But the cursor shows up at the default top-left position in the EditField. Can anybody tell me how I can draw the cursor where my text is? i.e. in the middle of EditField by calling drawText().

Upvotes: 2

Views: 4027

Answers (5)

Prasanth S
Prasanth S

Reputation: 3820

private EditField txtUserID;

txtUserID = new EditField("", null, 49, EDITABLE & EditField.NO_NEWLINE) {
            protected void layout(int width, int height) {
                // TODO Auto-generated method stub
                super.layout(200, 30);
            }

            protected void paint(Graphics g) {
                Border roundedBorder = BorderFactory
                        .createRoundedBorder(new XYEdges(9, 9, 9, 9),
                                Color.GRAY, Border.STYLE_SOLID);
                setBorder(roundedBorder);
                setBackground(BackgroundFactory
                        .createSolidBackground(Color.WHITE));
                if (getTextLength() == 0) {
                    g.setColor(Color.GRAY);
                    g.drawText("UserName", 0, 0);
                }
                g.setColor(Color.BLACK);
                super.paint(g);
            }

        };

Upvotes: 1

varun
varun

Reputation: 41

Thank you guys, i got one fine solution from Blackberry Journal.

public class ScrollingSearchBox extends HorizontalFieldManager
{
    private int managerWidth;
    private int managerHeight;
    public ScrollingSearchBox()
    {
        super(Manager.NO_HORIZONTAL_SCROLL);
        searchEdit = new BasicEditField(){
            public int getPreferredHeight()
            {                 
                return ret;
            }
            public int getPreferredWidth()
            {                    
                return ret;
            }
            public void paint(Graphics g)
            {
                getManager().invalidate();
                super.paint(g);                    
            }
        };

        HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
        {
            public void sublayout(int width, int height)
            {
                if (managerWidth == 0) {
                    managerWidth = searchEdit.getPreferredWidth();
                }
                if (managerHeight == 0) {
                    managerHeight = searchEdit.getPreferredHeight();
                }
                super.sublayout(managerWidth, managerHeight);
                setExtent(managerWidth,managerHeight);
            }
            public void paint(Graphics g) {
                super.paint(g);
            }
        };
        searchEdit.setMaxSize(70);
        hfm.add(searchEdit);
        add(hfm);
    }

    public int getPreferredHeight()
    {
        int ret = 0;            
        return ret;
    }
    protected void sublayout(int maxWidth, int maxHeight)
    {
        int currX = 0;
        int currY = 0;
        Field currField;

        currField = this.getField(0);
        switch (ScreenConfig.getInstance().getScreen())
        {
            case ScreenConfig.SCREEN_320_240:
            currX = 5;
            currY = 3;
            break;
            case ScreenConfig.SCREEN_480_360:
            case ScreenConfig.SCREEN_480_320:
            currX = 5;
            currY = 1;
            break;
        }
        this.setPositionChild(currField, currX, currY);
        this.layoutChild(currField, currField.getPreferredWidth(),
        currField.getPreferredHeight());
        setExtent(this.getPreferredWidth(), this.getPreferredHeight());
    }

    protected void paint(Graphics graphics)
    {
        super.paint(graphics);
        graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
    }
} 

I am giving this so it can help others. Keep sharing your codes. Thanks.

Upvotes: 2

Ahmet Gulden
Ahmet Gulden

Reputation: 2073

if you want to remove cursor you can override drawFocus of Field.

protected void drawFocus(Graphics graphics, boolean on) {} // remove cursor

Upvotes: 1

timoto
timoto

Reputation: 105

You can draw the cursor where you want it to rewriting the onFocus method of the editField.

        protected void onFocus(int direction) {
            super.onFocus(direction);
            this.setCursorPosition(this.getTextLength());
            invalidate();
        }

After getting the Focus, the field will position the cursor at the end of the text. The editField has setCursorPosition and also getTextLength.

Hope it helps.

Upvotes: 3

Vivart
Vivart

Reputation: 15313

I am not getting your question but.

  1. you can create single line edit field.

    BasicEditField editField = new BasicEditField(BasicEditField.NO_NEWLINE);

  2. you can set cursor position.

    editField.setCursorPosition(offset);

Upvotes: 4

Related Questions