2016-04-29 11 views
0

Vaadinコンボボックスのフィールド0をデフォルト値に設定しようとしています。ユーザーが何も選択しなかった場合、エラーメッセージは表示されません。Vaadinは0の位置でコンボボックスのフィールド値を取得できません

field.setNullSelectionAllowed(true); 
field.setNullSelectionItemId(container.getIdByIndex(0)); 

だから私はインデックスに空白値を持っていない。だから私は、私はそれを設定しようとこのとそれを管理している私は、インデックス0

でフィールドを埋めてきた代わりに空白のフィールドのことをしたいと思います0の代わりにインデックス1の私の以前の値がインデックス0になりました。そしてそれはまさに私が欲しいものであり、コンボボックスにも必要なものです。

しかし、残念ながら、フォームを送信すると、値が渡されません。インデックス0の後の値のみが渡されます。それはとてもイライラする、誰かが私を助けることができる? setNullSelectionItemIdに渡される値は100%存在します。

コンボボックスの場所0のインデックスから値を取得するにはどうすればよいですか?

p.s.ここに私のコードは次のとおりです。それは次のようにバインドされ

public Field<?> buildAndBindComboBox(final String caption, final BeanItemContainer<?> container, 
     final Object propertyId, final String title, final ValueChangeListener listener, final boolean nullAllowed, 
     final boolean required, final boolean enabled, final boolean visible) { 

    @SuppressWarnings("serial") 
    ComboBox field = new ComboBox(caption, container) { 
     // http://dev.vaadin.com/ticket/10544 
     // - typing in ComboBox causes Internal Error 
     private boolean inFilterMode; 

     @Override 
     public void containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent event) { 
      if (inFilterMode) { 
       super.containerItemSetChange(event); 
      } 
     } 

     @Override 
     protected List<?> getOptionsWithFilter(boolean needNullSelectOption) { 
      try { 
       inFilterMode = true; 
       return super.getOptionsWithFilter(needNullSelectOption); 
      } finally { 
       inFilterMode = false; 
      } 
     } 
    }; 

    field.setStyleName("comboBox"); 
    field.setInputPrompt("Select"); 
    if(defaultValue == true){ 
     field.setNullSelectionAllowed(false); 
     field.setNullSelectionItemId(container.getIdByIndex(0).toString()); 
     //field.select(container.getIdByIndex(0)); 
     //field.setValue(container.getIdByIndex(0)); 
     //field.setRequired(false); 
     defaultValue = false; 
    } else { 
     field.setNullSelectionAllowed(nullAllowed); 
     field.setRequired(required); 
    } 
    field.setImmediate(true); 
    field.setNewItemsAllowed(false); 
    field.setFilteringMode(FilteringMode.CONTAINS); 
    if (title != null) { 
     field.setItemCaptionPropertyId(title); 
    } 
    //field.setNullSelectionAllowed(nullAllowed); 
    //field.setRequired(required); 
    field.setVisible(visible); 

    if (listener != null) { 
     field.addValueChangeListener(listener); 
    } 

    this.bind(field, propertyId); 

    field.setEnabled(enabled); 

    return field; 
} 

public void setDefaultValueFirstItem(boolean def){ 
    defaultValue = def; 
} 

:私が正しくあなたの質問を理解している場合

commitmentFeeBinder.setDefaultValueFirstItem(true); 
commitmentFeeBinder.buildAndBindComboBox("No working day labels", noWorkingDays, "noWorkingDaysCF", "title", null, false, !transaCF, true, !transaCF); 
+0

あなたは、COMボックスに持っていない他のどのような項目?あなたは通常、インデックスではなくオブジェクトを直接扱います –

+0

ヌル選択を無効にし、最初にコンボボックスのsetValueを使ってインデックス0の項目を選択することができます。 –

+0

Steffen Harbichはすでにそれを試みました。私はselect()、setValue()、さまざまな組み合わせを試しました - 何も動作しません。 Andre、私は他のコンボボックスとテキストボックスとnoneコンボボックスを持っていますが、デフォルト値を選択する必要があります。私は空白の価値を持っている必要があり、それを選択する必要があるかのように思われる。私はVaadinでこの単純なデータを得るのは難しいとは思えない。 – DarioBB

答えて

1

、ステファンHarbichは、あなたがしたい場合は最初の項目はデフォルトで選択されることを示唆しているに正しいですヌル選択を無効にし、デフォルトで最初の項目を選択する必要があります。例えば。これは動作します:BeanItemContainerと

ComboBox cb = new ComboBox("", Arrays.asList("First", "Second", "Third")); 
cb.setNullSelectionAllowed(false); 
cb.select("First"); 

または代わり:

List<MyBean> beans = Arrays.asList(new MyBean("First"), new MyBean("Second"), new MyBean("Third")); 
BeanItemContainer<MyBean> bic = new BeanItemContainer<>(MyBean.class, beans); 
ComboBox cb = new ComboBox("", bic); 
cb.setNullSelectionAllowed(false); 
cb.select(bic.getIdByIndex(0)); 
0
private void resetComboBoxToIndex(ComboBox combo, int index) { 
    BeanItemContainer<Bean_ComboBox> items_combo = (BeanItemContainer<Bean_ComboBox>)combo.getContainerDataSource(); 
    if(items_combo != null && items_combo.size() > index) { 
     Bean_ComboBox primerItem = items_combo.getIdByIndex(index); 
     if(primerItem != null) { 
       combo.select(primerItem); 
     } 
    } 
} 
+0

ちょっとしたコードを投げるのではなく、問題の内容とその修正方法についても説明する必要があります。これにより、元のポスターだけでなく将来の訪問者にとっても、あなたの答えがより価値があります。 – miken32

+0

OK。私が試してみます。このメソッドを使用すると、パラメータとしてインデックスを示すコンボボックスの項目を選択できます。 Java Swingのcombobox.selectedIndex(n)と考える –

関連する問題