Pergunta

I'm working on getting the value of a textfield from another class using Vaadin.

my solution is something like this.

public class MyTextField{

    String str;

    public MyTextField(){
        TextField text = new TextField();
        str = text.getValue().toString();
    }

    public String getStr(){
        return str;
    }    
}

public class MyButton{    
    public MyButton(){
        Button button = new Button("UPDATE");
        button.addListener(new Button.ClickListener(){
            @Override
        public void buttonClick(ClickEvent event) {
                MyTextField text = new MyTextField();
                System.out.println(text.getStr());   
            }
        });
    }    
}

the problem is, I always get null result. any help is highly appreciated. Thank you.

Foi útil?

Solução 2

I would rather do some thing like this, to achieve what you are doing. This is based on the assumption that you are trying to read the user entered value on click of a button. There are two option to achieve this.

Option 1

     public class MyTextField extends TextField{


        public MyTextField(){
        }

        public String getStr(){
            return this.getValue();
        }
    }


    public class MyButton extends Button{

        public MyButton(){
        setCaption("UPDATE");
        }
    }

   //Add in your presentation logic

   final MyTextField text = new MyTextField();
    MyButton button = new MyButton();
    mainLayout.addComponent(text);
    mainLayout.addComponent(button);
    button.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            System.out.println(text.getStr());
        }
    });

Option 2

public  class MyTextField extends TextField{


    public MyTextField(){
    }

    public String getStr(){
        return this.getValue();
    }
}


public class MyButton extends Button{

    public MyButton(final MyTextField text){
    setCaption("UPDATE");
    addClickListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                System.out.println(text.getStr());
            }
       });
    }
}
//And in your presentation logic 

    final MyTextField text = new MyTextField();
    MyButton button = new MyButton(text);
    mainLayout.addComponent(text);
    mainLayout.addComponent(button);

Please let me know if in case if this is not what you are expecting.

And Kudos to Hovercraft Full Of Eels for trying to help him out

Thanks.

Outras dicas

In all likelihood the MyTextField object inside of your buttonClick method is not the same MyTextField that is being displayed to the user. A solution is to get a reference to the class that holds the displayed MyTextField, and then call a public method on that reference to extract the text.


Edit 1
Note that I'm not familiar with Vaadin, but your MyTextField class looks suspicious in that you're declaring the TextField inside of the constructor making it visible only in the constructor. I'm also not sure what calling getValue() on it immediately and before any event, before the user's had a chance to interact with the component, will do. This looks very suspicious to me.


Edit 2

For instance, if this were Swing, I'd do something like:

public class MyTextField{

    TextField textField = new TextField();

    public MyTextField(){
    }

    // so it can be added to the gui
    public TextField getTextField() {
      return textField;
    }


    public String getStr(){
        return textField.getValue().toString(); // not sure if this is correct
    }    
}

public class MyButton{  
   private MyTextField textField;
   private Button button = new Button("UPDATE");

   public MyButton(final MyTextField textField){
      this.textField = textField;
      button.addListener(new Button.ClickListener(){
         @Override
         public void buttonClick(ClickEvent event) {
            System.out.println(textField.getStr());   
         }
      });
    }    
}

Edit 3
You ask:

you have declared "private MyTextField textfield" as a field and add a parameter (MyTextField textfield) from the MyButton constructor. does it mean that I have to pass the class MyTextField to the MyButton class as a reference?

Again, note that I am not familiar with Vaadin, but I am familiar with general use of Java objects, and this I know. If you want to have one class (here MyButton) get the state of another object (here MyTextField), then the first object needs a valid reference to the second. One way to get this reference is by passing it in as a constructor parameter. Another is via a setter method, say by giving MyButton a setMyTextField(TextField textField) method, and then assigning the reference to your field. Another option is via "dependency injection", but I think that this may be a little too advanced for now, but do consider it in the future.

If you try to do it as you're doing it in your original post, things will likely fail, since sure, you're giving MyButton a MyTextField object, but what MyTextField object is this? It's one that has been created in the MyButton class and is present no where else.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top