views:

1450

answers:

3

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?

+1  A: 

Yes, you can override the paint method of each UI Element

An example:

public class WhiteLabelField extends LabelField 
{
    public WhiteLabelField()
    {
     super();
    }
    public WhiteLabelField(ObjectGroup text)
    {
     super(text);
    }
    public WhiteLabelField(Object text, long style)
    {
     super(text, style);
    }
    public void paint(Graphics _g)
    {
        _g.setColor(Color.WHITE);
        super.paint(_g);
    }
    // Custom
    public void setSmallFontSize()
    {
     setFont( Font.getDefault( ).derive( Font.PLAIN, 16 ));
    }
}
Henrik P. Hessel
+5  A: 

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);
Max Gontar
Multiline labels are really not a problem, I had another problem that made me think it didn't work.As for the TextField - indeed what you suggest makes a no-caret TextField; however, it's not scrollable - so if the text in the field is too long to fit on screen, it cannot be viewed.
noamtm
see updated code
Max Gontar
Thanks for the great answer!
noamtm
You're welcome!
Max Gontar
A: 

You could also use a RichTextField with a style of Field.NON_FOCUSABLE and you will have what you want I think...

DB