1

I have a TextField in a Form. This TextField should have focus by default, which works fine. Now I'd like the user to be aware of that and show him, that he's inside the TextField - so the TextField cursor should be shown and blink.

I only found drawTextFieldCursor in DefaultLookAndFeel. but I have absolutely no idea how to apply this to my TextField.

Any help - and code would be appreciated!


Here's a sample. I still don't have it working.

public void search2() {
    searchForm                      = new Form();
    TextField searchArea = new TextField();
    searchForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    searchForm.addComponent(searchArea);
    searchArea.requestFocus();
    int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
    searchArea.keyPressed(i);
    searchArea.keyReleased(i);
    searchForm.show();
}

What happens is: the TextField is focused, it can be directly edited, the "Abc" mode is shown, but what I really want is to show the user the cursor, so he KNOWS he's inside the TextField. This is not happening... if someone could show me some working code for that...

Carl
  • 215
  • 1
  • 11

2 Answers2

2

You want the text field to be in editing mode not for the text field cursor to show (which happens when editing). Use requestFocus() to make sure the text field has focus then use something like:

int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
tf.keyPressed(i);
tf.keyReleased(i);
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • That's pretty odd since I see it blinking in pretty much every application. E.g. in the LWUIT demo it blinks without any hack or patch for the current focused component. I suggest you confirm this in the LWUIT demo and then just place a breakpoint in LWUIT and see why the cursor isn't drawn for you. It should be seamless. – Shai Almog Oct 04 '11 at 17:35
0

The drawTextFieldCursor method has a Graphics parameter , so the way you can draw your cursor is :

  1. derive TextField
  2. in its public void paint(Graphics g) you call drawTextFieldCursor after setting the drawing methods of the TextField's paint Graphic.
  • I triedpublic class TF extends TextField { public void paint(Graphics g) { UIManager manager = getUIManager(); manager.getLookAndFeel().drawTextField(g, this); manager.getLookAndFeel().drawTextFieldCursor(g, this); paintHint(g); } } and modified paintHint in TextArea to protected void paintHint(Graphics g) {, but still no blinking cursor. – Carl Sep 20 '11 at 12:51
  • In the Javadoc it is said that `blinking is handled simply by avoiding a call to this method.` –  Sep 20 '11 at 13:34
  • public class TF extends TextField { public void paint(Graphics g) { UIManager manager = getUIManager(); manager.getLookAndFeel().drawTextField(g, this); paintHint(g); } } doesn't blink, either. What this did: is it removes the cursor completely - but I want it ALL the times – Carl Sep 20 '11 at 13:52