2016-04-21 36 views
1

Vaadin 7.6.5。 我想弄清楚为什么在下面的情况下值更改侦听器失败? CheckBox正在观察addValueChangeListener OptionGroup。OptionGroup addValueChangeListener CheckBox

@Theme("vaadindemo") 
public class VaadindemoUI extends UI { 

    @WebServlet(value = "/*", asyncSupported = true) 
    @VaadinServletConfiguration(productionMode = false, ui = VaadindemoUI.class) 
    public static class Servlet extends VaadinServlet { 
    } 

    @Override 
    protected void init(VaadinRequest request) { 
     final VerticalLayout layout = new VerticalLayout(); 
     layout.setMargin(true); 
     setContent(layout); 

     OptionGroup group = new OptionGroup(); 
     group.addItem("01"); 
     group.setItemCaption("01", "ONE"); 

     group.addItem("02"); 
     group.setItemCaption("02", "TWO"); 

     group.addItem("03"); 
     group.setItemCaption("03", "THREE"); 
     group.addValueChangeListener(new ValueChangeListener() { 

      @Override 
      public void valueChange(ValueChangeEvent event) { 
       System.out.println("group getValue " + event.getProperty().getValue()); 
       System.out.println("group getType " + event.getProperty().getType()); 
      } 
     }); 
     CheckBox box = new CheckBox(); 
     box.setCaption("Check Me"); 
     //Notify CheckBox of value change of radio button 

     //group.addValueChangeListener(box); -- // Code Fails 
     //box.addValueChangeListener(group); // Selected Radio Button is removed 


     TextField field = new TextField(); 
     field.addValueChangeListener(new ValueChangeListener() { 

      @Override 
      public void valueChange(ValueChangeEvent event) { 
       System.out.println("field getValue " + event.getProperty().getValue()); 
       System.out.println("field getType " + event.getProperty().getType()); 

       System.out.println("field getType " + field.getValue()); 
      } 
     }); 


     group.addValueChangeListener(field);// The value is reflected. How do i get only the event without over writing the value 

     //group.addValueChangeListener(box); 

     layout.addComponent(group); 
     layout.addComponent(box); 
     layout.addComponent(field); 

     Button button = new Button("Click Me"); 
     button.addClickListener(new Button.ClickListener() { 
      public void buttonClick(ClickEvent event) { 
       layout.addComponent(new Label("Thank you for clicking")); 
      } 
     }); 
     layout.addComponent(button); 
    } 

} 

它记录:

 
com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.lang.String to presentation type class java.lang.Boolean. No converter is set and the types are not compatible. 
    at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:116) 
    at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:736) 
    at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:721) 
    at ... 
+0

我想知道是什么你是在试图达到那种约束力?如你所见,绑定一个OptionGroup和一个CheckBox是不可能的。 –

+0

嗨,亨利,我看着观察者模式。我需要一个表单中的所有组件来查找OptionGroup的值更改。 OptionGroup的值更改应该触发其他组件更新其状态。 addValueChangeListener适用于TextBox。我不知道为什么值属性试图映射到CheckBox(布尔值),因为CheckBox只对接收事件感兴趣。 – Ravi

+0

代替CheckBox,我添加了一个TextField。当我点击CheckBox时,CheckBox的值显示在TextField中。我正在寻找的是事件并且对价值不感兴趣。 – Ravi

回答

0

我想你想的东西像...

group.addValueChangeListener(new ValueChangeListener() { 

      @Override 
      public void valueChange(ValueChangeEvent event) { 

       //Do something with box here? e.g... 
       box.setValue(!box.getValue()); 

      } 
     }); 

,而不是...

​​
+0

我不知道在屏幕上会有多少表单元素。因此,屏幕上的所有组件都会观察该组。AbstractField addValueChangeListener调用 - > AbstractClientConnector addListener(AbstractField.ValueChangeEvent.class,listener, VALUE_CHANGE_METHOD); – Ravi

+0

AbstractClientConnector addListener(AbstractField.ValueChangeEvent.class,listener, VALUE_CHANGE_METHOD); JavaDoc 使用指定的激活方法注册新的侦听器,以侦听由此组件生成的事件 。如果激活方法没有任何参数 ,则在调用 时事件对象不会传递给它。 – Ravi

+0

为了澄清,您是在说您不知道您的选项组中有多少组件? –