Get TextField value - not using GUI builder

33 views
Skip to first unread message

Dan

unread,
Oct 21, 2014, 11:24:48 AM10/21/14
to codenameone...@googlegroups.com
I'm using Eclipse and writting things by hand without the use of the GUI builder.

Here's my main form:

public class WelcomeForm extends Form {

    Button btnBrowse;
    Button btnLogin;
    Label lblUsername;
    Label lblPassword;
    Label lblErrorMessage;   
    TextField txtUsername;
    TextField txtPassword;
   
    public WelcomeForm(){
        init();
    }
   
    private void init(){
        btnBrowse = new Button("Browse Games");
        btnLogin = new Button("Login");
        btnBrowse.addActionListener(new BrowseButtonActionListener(this));
        btnLogin.addActionListener(new LoginButtonActionListener(this));
       
        lblUsername = new Label("Username:");
        lblPassword = new Label("Password:");
        lblErrorMessage = new Label("");
       
        txtUsername = new TextField();
        txtPassword = new TextField();

        setLayout(new FlowLayout(Component.CENTER));
        setTitle("My title");
        addComponent(lblUsername);
        addComponent(txtUsername);
        addComponent(lblPassword);
        addComponent(txtPassword);
        addComponent(btnLogin);
        addComponent(lblErrorMessage);
        addComponent(btnBrowse);
    }
}

Here is my listener:

public class LoginButtonActionListener implements ActionListener {

    Form frm;

    public LoginButtonActionListener(Form frm) {
        this.frm = frm;
    }

    public void actionPerformed(ActionEvent evt) {
        //looking at reading form elements here
        System.out.println("Login button clicked!!");
    }
}

How would I get at the TextField in the WelcomeForm class via the listener?

On a side note, how typical is it to develop in CN1 this way?

Dan

unread,
Oct 21, 2014, 11:54:59 AM10/21/14
to codenameone...@googlegroups.com
Did a bit of digging and this seems to work in the actionPerformed method:

TextField f = (TextField)this.frm.getContentPane().getComponentAt(1);

You do have to know the indexes though. Again, how typical is this sort of approach?

Maaike

unread,
Oct 22, 2014, 4:25:35 AM10/22/14
to codenameone...@googlegroups.com
You can just do txtUsername.getText() within actionPerformed(). Or didn't you use it as an inline class? Otherwise, I would say make getters so you can access the textfield with getTxtUsername(); 

(I would also make my attributes private because they now can be changed outside the class by other methods within the same package, but maybe that has a reason. Since your attributes are not private, you can maybe just access them from the class, welcomeForm.txtUsername).

Shai Almog

unread,
Oct 23, 2014, 12:18:21 AM10/23/14
to codenameone...@googlegroups.com
You should use:

     TextField tf = (TextField)evt.getSource();
    
Relying on the position of the component within the content pane is pretty fragile.

We program by hand quite a bit e.g. see the kitchen sink demo.

Maaike

unread,
Oct 23, 2014, 3:56:27 AM10/23/14
to codenameone...@googlegroups.com
Since the listener is on the button, don't you get the button with evt.getSource() instead of the textfield?

Shai Almog

unread,
Oct 23, 2014, 8:54:19 AM10/23/14
to codenameone...@googlegroups.com
Yes you are correct, I misread that.
In that case the only reasonable suggest is the one you made.
Reply all
Reply to author
Forward
0 new messages