Mario41
(Mário Korčák)
March 4, 2013, 9:08am
1
Hi all.
I’m working with table with (for example) 4 columns. Label, ComboBox, CheckBox and again Label. The ComboBox contains for example 2 choices: Test and Other. When user choose Other the CheckBox in that row will be invisible and when i choose Test the CheckBox will be visible. Here is the code:
package com.example.table;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
public class TableUI extends UI {
private HorizontalLayout horizontalLayout;
@Override
public void init(VaadinRequest request) {
horizontalLayout = new HorizontalLayout();
horizontalLayout.setSizeFull();
setContent(horizontalLayout);
addTable();
}
private void addTable() {
Table table = new Table();
table.setEditable(true);
table.setImmediate(true);
initContainer(table);
addComponents(table);
table.setSizeFull();
horizontalLayout.addComponent(table);
}
private void initContainer(Table table) {
HierarchicalContainer hierarchicalContainer = new HierarchicalContainer();
hierarchicalContainer.addContainerProperty("Label", Label.class, null);
hierarchicalContainer.addContainerProperty("Combo", ComboBox.class, null);
hierarchicalContainer.addContainerProperty("Check", CheckBox.class, null);
hierarchicalContainer.addContainerProperty("Label2", Label.class, null);
table.setContainerDataSource(hierarchicalContainer);
}
@SuppressWarnings("unchecked")
private void addComponents(Table table) {
HierarchicalContainer hierarchicalContainer = (HierarchicalContainer) table.getContainerDataSource();
Object item = null;
for (int i = 1; i <= 1; i++) {
item = table.addItem();
hierarchicalContainer.getContainerProperty(item, "Label").setValue(getLabel(i));
hierarchicalContainer.getContainerProperty(item, "Combo").setValue(getCombo(i, hierarchicalContainer, table));
hierarchicalContainer.getContainerProperty(item, "Check").setValue(getCheck(i));
hierarchicalContainer.getContainerProperty(item, "Label2").setValue(getLabel(i));
}
}
private Label getLabel(int i) {
Label label = new Label(String.valueOf(i));
label.setSizeUndefined();
return label;
}
private ComboBox getCombo(int i, final HierarchicalContainer hierarchicalContainer, final Table table) {
ComboBox comboBox = new ComboBox();
comboBox.addItem("Test");
comboBox.addItem("Other");
comboBox.setData(i);
comboBox.setImmediate(true);
comboBox.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
ComboBox combo = (ComboBox) event.getProperty();
Object o = combo.getData();
Object value = combo.getValue();
if ("Other".equals(value)) {
Property containerProperty = hierarchicalContainer.getContainerProperty(o, "Check");
CheckBox checkBox = (CheckBox) containerProperty.getValue();
checkBox.setVisible(false);
((Label)hierarchicalContainer.getContainerProperty(o, "Label2").getValue()).setVisible(false);
} else {
Property containerProperty = hierarchicalContainer.getContainerProperty(o, "Check");
CheckBox checkBox = (CheckBox) containerProperty.getValue();
checkBox.setVisible(true);
((Label)hierarchicalContainer.getContainerProperty(o, "Label2").getValue()).setVisible(true);
table.markAsDirty();
}
}
});
return comboBox;
}
private CheckBox getCheck(int i) {
CheckBox checkBox = new CheckBox();
return checkBox;
}
}
Problem is that when i choose Other and next time i choose Test the CheckBox is still invisible. (the debugger show that the property visible on the CheckBox component is set on correct value (true or false) but the Table don’t show it.
Do you know what to do or which way can i go to solve this problem?
Thanks