1 Reply Latest reply: Feb 16, 2012 4:10 AM by Umer RSS

    Focus remains in Button when showing Form

    392544
      Hi all,

      I have a Form into which there are Container, GridLayout, Label's, TextField's, and Button's.
      Here is the code :
      package hello;

      import com.sun.lwuit.*;
      import com.sun.lwuit.events.ActionEvent;
      import com.sun.lwuit.events.ActionListener;
      import com.sun.lwuit.layouts.GridLayout;
      import com.sun.lwuit.plaf.UIManager;
      import com.sun.lwuit.util.Resources;
      import javax.microedition.midlet.MIDlet;
      import java.util.Hashtable;
      import javax.microedition.rms.*;

      public class HelloMIDlet extends MIDlet implements ActionListener {

      private Form form;
      private Command ajoutCommand,exitCommand;
      private Container panel;
      private GridLayout layout;
      private Label cin,nom,prenom,adresse;
      private TextField cinField,nomField,prenomField,adresseField;
      private Button btnAjout, btnExit;
      private Hashtable tTheme;
      private RecordStore base;
      private String dataDB;

      public HelloMIDlet() {

      }

      public void startApp() {
      Display.init(this);
      try
      {
      Resources r = Resources.open("/resources.res");
      tTheme = r.getTheme("Theme");
      UIManager.getInstance().setThemeProps(tTheme);
      }
      catch (java.io.IOException e)
      {
      System.err.println("error reading resource file !");
      }

      try {
      base = RecordStore.openRecordStore("LWUITBase", true);
      }
      catch (RecordStoreException ex) {
      ex.printStackTrace();
      }

      form = new Form("test");
      panel = new Container();
      layout = new GridLayout(4,2);
      panel.setLayout(layout);

      ajoutCommand = new Command("Ajouter",1);
      exitCommand = new Command("Exit", 2);
      //form.addCommand(ajoutCommand);
      //form.addCommand(exitCommand);

      cin = new Label("CIN :");
      cinField = new TextField();
      //cinField.setConstraint(TextArea.NUMERIC);
      nom = new Label("Nom :");
      nomField = new TextField();
      prenom = new Label("Prénom :");
      prenomField = new TextField();
      adresse = new Label("Adresse :");
      adresseField = new TextField();
      btnAjout = new Button(ajoutCommand);
      btnAjout.addActionListener(this);
      btnExit = new Button(exitCommand);
      btnExit.addActionListener(this);

      panel.addComponent(cin);
      panel.addComponent(cinField);
      panel.addComponent(nom);
      panel.addComponent(nomField);
      panel.addComponent(prenom);
      panel.addComponent(prenomField);
      panel.addComponent(adresse);
      panel.addComponent(adresseField);
      panel.addComponent(btnExit);
      panel.addComponent(btnAjout);

      cinField.setFocus(true);

      form.addComponent(panel);

      form.show();
      }

      public void pauseApp() {
      }

      public void destroyApp(boolean unconditional) {
      notifyDestroyed();
      }

      public void actionPerformed(ActionEvent evt) {
      if (evt.getCommand() == exitCommand)
      destroyApp(false);
      else
      {
      if (evt.getCommand() == ajoutCommand)
      insert();
      }
      }

      public synchronized void insert()
      {
      String record = cinField.getText() + ";" + nomField.getText() + ";" + prenomField.getText() + ";" + adresseField.getText();
      byte[] bytes = record.getBytes();
      try {
      base.addRecord(bytes, 0, bytes.length);
      cinField.setText("");
      nomField.setText("");
      prenomField.setText("");
      adresseField.setText("");
      }
      catch (RecordStoreException ex) {
      ex.printStackTrace();
      }
      RecordEnumeration enumRec = null;
      try {
      enumRec = base.enumerateRecords(null, null, false);
      } catch (RecordStoreNotOpenException ex) {
      ex.printStackTrace();
      }
      while(enumRec.hasNextElement())
      {
      try {
      dataDB = new String(enumRec.nextRecord());
      } catch (RecordStoreException ex) {
      ex.printStackTrace();
      }
      Dialog.show("Confirm","data = "+dataDB,"Ok","Cancel");
      }
      }
      }

      So when running the application then the cursor is on the Exit button but not on the "cinField" field as I coded.

      So how to make the cursor in the "cinField" field after showing the Form ?

      Another problem : when I set a NUMERIC constraint to the "cinField" field then when I try to enter a value in it then I cannot type numbers , and I must click the T9 command ! So how to make the application ready to accept numbers in that situation ?

      Thank you very much indeed