dynamic button creation to the form using lwuit 1.4
public class StateMachine extends StateMachineBase {
public Container con1;
protected void beforeMainForm(Form f) {
con1 = findMenucon(f);<Br>
super.beforeMainForm(f);<br>
}
}
//class mainmidlet()
public void run() {
try {
//new StateMachine("/App.res");
new mainform("/App.res");
} catch (Exception ex) {
ex.printStackTrace();
}
}
class mainform implements ActionListener{
Vector bname;
Button[] b;
String mainmenu=null;
Form frm;
mainform(String string) {
try {
Resources res = Resources.open(string);
UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
UIBuilder builder = new UIBuilder();
frm = (Form)builder.createContainer(res, "MainForm");
StateMachine开发者_开发百科 sm=new StateMachine("/App.res");
System.out.println("------->>>");
bname=new Vector();
this.readmenu();
b = new Button[bname.size()];
System.out.println(b.length+bname.toString());
for (int i = 0; i<b.length; i++) {
b[i] = new Button(bname.elementAt(i).toString());
b[i].setAlignment(Label.CENTER);
b[i].getStyle().setMargin(2,5,5,5);
b[i].getStyle().setPadding(5,5,5,5);
System.out.println(b[i].toString());
b[i].addActionListener(this);
sm.con1.addComponent(b[i]);
//System.out.println("\n " + b[i]);
}
frm.addComponent(sm.con1);
frm.show();
}
catch(IOException err) {
err.printStackTrace();
}
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
I am using the above code for creation dynamic Button
using json. i can get the value in console but could not get the Button
in the Form
That's clearly incorrect use of the state machine (which isn't a part of 1.4 BTW) since the base class will create and show a form on its own and set its own theme (overriding whatever you did before).
You also neglected to include the creation/definition of con1
and included calls to UnsupportedOperationException
which won't run on the device.
All your LWUIT code should be in the state machine, please follow the demos including the t-zone demo where we replace the title on the fly to create an animation. All of that code is entirely in the state machine where we override the specific form initialization methods and modify the form from there.
If you want to write everything manually don't use the state machine, just use UIBuilder directly.
I think you should after constructing your Buttons, you can still add them to the Form even after show, the do a revalidate() on the Form
frm.revalidate();
精彩评论