Skip to main content

Focus remains in Button when showing Form

2 replies [Last post]
andrianiaina
Offline
Joined: 2011-05-18
Points: 0

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
andrianiaina
Offline
Joined: 2011-05-18
Points: 0

The focus problem is resolved by writing form.setFocused(cinField) after showing the form.

Now how to make the application ready to accept numbers in the cinField field when it is constrained with NUMERIC ?

vprise
Offline
Joined: 2003-11-07
Points: 0

Use requestFocus() onto the text field.