English 中文(简体)
我如何利用Vadin表格的成分使电池/残疾。
原标题:How can I enable/disable cells using Vaadin table component?
  • 时间:2011-09-23 22:47:57
  •  标签:
  • vaadin

I have a table with 2 columns: a checkbox and a textfield. I want to disable the textfield depending of the respective (same row) checkbox status. If the checkbox is checked then the textfield will be cleared and be read only. Is this possible ? Here is my code:

@SuppressWarnings("serial")
private Table filtersTable() {
    final Table table = new Table();
    table.setPageLength(10);
    table.setSelectable(false);
    table.setImmediate(true);
    table.setSizeFull();
    // table.setMultiSelectMode(MultiSelectMode.SIMPLE) ;
    table.addContainerProperty("Tipo filtro", CheckBox.class, null);
    table.addContainerProperty("Valor", String.class, null);
    table.setEditable(true);
    for (int i = 0; i < 15; i++) {
        TextField t = new TextField();
        t.setData(i);
        t.setMaxLength(50);
        t.setValue("valor " + i);
        t.setImmediate(true);
        t.setWidth(30, UNITS_PERCENTAGE);
        CheckBox c = new CheckBox(" filtro " + i);
        c.setWidth(30, UNITS_PERCENTAGE);
        c.setData(i);
        c.setImmediate(true);
        c.addListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
              // within this, could I access the respective row ID
              // (i) then enable/disable TextField t on second column ?
              System.out.println("event.getProperty().getValue()="
                        + event.getProperty().getValue());
            }
        });
        table.addItem(new Object[] { c, t }, i);
    }
    return table;
}

Thanks

问题回答

Few changes to your code made it possible. Not the finiest way, but te simpliest.

首先,我们必须将第二栏(Valor)定在<代码>Text Field.}上,而不是<编码>String.nal。

更改如下:

table.addContainerProperty("Valor", TextField.class, null);

Instead of keepin the variable i in the CheckBox.setData(), I suggest you to link your checkBox to the TextField of the same row, like this :

c.setData(t);

Finally I made little change to your listener :

c.addListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {

                CheckBox checkBox = (CheckBox)event.getProperty();
                if((Boolean) checkBox.getValue())
                {
                    TextField associatedTextField = (TextField)checkBox.getData();

                    //Do all your stuff with the TextField
                    associatedTextField.setReadOnly(true);
                }
            }
        });

Hope it s work for you!

Regards, Éric

    public class MyCheckBox extends CheckBox {

    private TextBox t;

    public MyCheckBox(TextBox t) {

     this.t = t;
    attachLsnr();
    }

    private void attachLsnr()
    {
     addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {

                    CheckBox checkBox = (CheckBox)event.getProperty();
                    if((Boolean) checkBox.getValue())
                    {
                        t.setReadOnly(true);
                    }
                }
            });


    }

 }




相关问题
Client Browser detection in Vaadin

I want to set different themes to my Vaadin application, depending on the user agent. In particular I want to distinguish at least between mobile devices (iPhone, Android,...) and desktop web browser. ...

Using Clojure with Vaadin

Has anyone tried implementing a web application with Clojure ( using Compojure ) and Vaadin ? I had seen an article on using Clojure with JWT for creating web apps. Vaadin is based on GWT so you get a ...

How to change the tabbar color in vaadin?

Hi want to change the tab color when the tab get focus in vaadin?can any one help me how to customize tabsheet to achive this..

采用Vadin的植被

能否在《维也纳条约法公约》申请范围内使用瓦亚丁框架中的植被?

热门标签