Search code examples
user-interfaceblackberrytextfieldcaretlabelfield

Blackberry Java: TextField *without* the caret?


I want a non-editable TextField (or a subclass) that doesn't even have the caret displayed. Alternatively, I want a multiline LabelField. Is any of these possible?


Solution

  • TextField without focus cursor

    TextField readOnly = new TextField(NON_FOCUSABLE);
    readOnly.setText("Read only, no carret");
    add(readOnly);
    

    TextField drawFocus override

    If text is too large to fit the screen, you can override drawFocus method in TextField, so scrolling will be available:

    TextField readOnly = new TextField(READONLY)
    {
        protected void drawFocus(Graphics graphics, boolean on) {}
    };
    

    TextFields, separated with NullFields

    Other option is to split TextField into several, separated with NullFields:

    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. Ut wisi enim ad minim veniam, quis "
                    + "nostrud exerci tation ullamcorper suscipit "
                    + "lobortis nisl ut aliquip ex ea commodo consequat. "
                    + "Duis autem vel eum iriure dolor in hendrerit in "
                    + "vulputate velit esse molestie consequat, vel "
                    + "illum dolore eu feugiat nulla facilisis at vero "
                    + "eros et accumsan et iusto odio dignissim qui "
                    + "blandit praesent luptatum zzril delenit augue "
                    + "duis dolore te feugait nulla facilisi.";
    
            text = addScrollText(text, 150);
        }
    
        private String addScrollText(String text, int partSize) {
            while (0 < text.length()) {
                int len = Math.min(partSize, text.length());
                TextField readOnly = new TextField(NON_FOCUSABLE);
                readOnly.setText(text.substring(0, len));
                add(readOnly);
                add(new NullField());
                text = text.substring(len);
            }
            return text;
        }
    }
    

    Multiline LabelField

    Multiline text in LabelField, just use newline escape character:

    String text = "first line \nnew line \nanother line";
    LabelField multiLine = new LabelField(text);
    add(multiLine);