LWUIT (J2ME) please help me and guide me to finish this task

Discussion in 'Java' started by focode, Sep 8, 2009.

  1. focode

    focode Guest

    i have to develop a GUI based menu , for that i have set the layout to
    gridLayout 2,2 placed label with images in all four of it , than
    placed two button "ok" and "Exit"

    what i have achieved till now :

    1.when focus changes from one label to another then program prints the
    details of the component to which it shifted including the text
    included in the label (below the image ) .
    2. when softbutton "ok" or "exit is triggered" i am able to perform
    some task (like i print the name of the command)

    What i am required to do now:

    1. when focus is shifted from one component to another , i get the
    full detail like the height , width , text etc , is their any
    standered way to get the present focusble component , by which i can
    differentiate it from other component(Label) in the form , (their are
    4 label and i have also placed text below the images which is shown
    when "String text = f1.getFocused().toString();" is called )

    2. when focus changes from one Label to another , then it is not
    visible on the mobile screen (like if focus changes than present focus
    should be surrounded by squire margin so that end user will know which
    is the current selected item on screen )

    3. Finally, how to trigger a method based upon present select when
    user hits soft button "ok" or "exit".

    here is the source code --


    import com.sun.lwuit.Command;
    import com.sun.lwuit.Component;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Image;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.events.FocusListener;
    import com.sun.lwuit.layouts.GridLayout;
    import java.io.IOException;
    import javax.microedition.midlet.MIDlet;


    public class Semifinal extends MIDlet implements ActionListener,
    FocusListener{
    public static String CurrentFocus;
    Form f1;
    public void startApp() {
    Display.init(this);
    getForm();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    public Form getForm()
    {
    try {

    f1 = new Form("Text");

    f1.setLayout(new GridLayout(2,2));
    Image i1 = Image.createImage("/res/girl2-32.png");

    Label l1 = new Label(i1);
    l1.setText("Hello");
    l1.setTextPosition(Component.BOTTOM);
    l1.setAlignment(Component.CENTER);
    l1.setAlignment(Component.CENTER);
    l1.getStyle().setMargin(10, 10, 10, 10);
    l1.setFocusable(true);
    l1.addFocusListener(this);

    Command ok = new Command("Ok");
    f1.addCommand(ok);
    Command exit = new Command("Exit");

    f1.addCommand(exit);
    f1.addComponent(l1);

    Label l2 = new Label(i1);
    l2.setText("How");
    l2.setTextPosition(Component.BOTTOM);
    l2.setAlignment(Component.CENTER);
    l2.setAlignment(Component.CENTER);
    l2.getStyle().setMargin(10, 10, 10, 10);
    l2.setFocusable(true);
    l2.addFocusListener(this);
    f1.addComponent(l2);

    Label l3 = new Label(i1);
    l3.setText("Are");
    l3.setTextPosition(Component.BOTTOM);
    l3.setAlignment(Component.CENTER);
    l3.setAlignment(Component.CENTER);
    l3.getStyle().setMargin(10, 10, 10, 10);
    l3.addFocusListener(this);
    l3.setFocusable(true);
    f1.addComponent(l3);

    Label l4 = new Label(i1);
    l4.setText("U!");
    l4.setTextPosition(Component.BOTTOM);
    l4.setAlignment(Component.CENTER);
    l4.setAlignment(Component.CENTER);
    l4.getStyle().setMargin(10, 10, 10, 10);
    l4.setFocusable(true);
    l4.addFocusListener(this);
    f1.addComponent(l4);
    f1.setCommandListener(this);
    f1.setScrollable(false);
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }
    f1.show();
    return f1;
    }


    public void actionPerformed(ActionEvent ae) {
    Form current = Display.getInstance().getCurrent();
    if(current == f1)
    {
    String st = f1.getFocused().toString();
    System.out.println(st);
    String t2 = f1.getFocused().toString();


    Command c = ae.getCommand();
    String str = c.getCommandName();
    if(str.equals("Ok"))
    {
    System.out.println(" You Printed ok button");
    // System.out.println(" CUrrent Selcetion is "+t2 );
    }
    System.out.println(str);

    }

    }


    public void focusGained(Component arg0) {

    String text = f1.getFocused().toString();
    if(arg0 instanceof Label)
    {
    String text2 = arg0.toString();
    System.out.println(text2);
    }
    System.out.println(CurrentFocus);
    System.out.println(text);

    }

    public void focusLost(Component arg0) {
    System.out.println("lost");
    }

    }
     
    focode, Sep 8, 2009
    #1
    1. Advertisements

  2. I have no experience with com.sun.lwuit, but here is an example of using
    a FocusListener to highlight the selected element in a grid of
    JTextField instances:

    <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/
    src/org/gcs/robot/RCKeys.java>
    <http://robotchase.sourceforge.net/org/gcs/robot/RCKeys.html>

    I always find it helpful to review the relevant tutorial, too:

    <http://java.sun.com/docs/books/tutorial/uiswing/events/
    focuslistener.html>
     
    John B. Matthews, Sep 8, 2009
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.