0

I want to handle Property.ValueChangeEvent in my IndexedContainer, when the value is changed in specific row. I expected the "event" passed as argument will supply the information what has been changed.

IndexedContainer container = new IndexedContainer();
.
. add container properties, insert items ...
.
container.addValueChangeListener(this)
.
.
public void valueChange(ValueChangeEvent event) {
  // How to get itemId and another properties from the same item?
  event.getProperty(); // This returns only whole container
}

The getProperty() function doesn't give me the information, from which row and column it comes. Thanks for help.

JH

Hink
  • 1,054
  • 1
  • 15
  • 31

2 Answers2

1

Usually a container is used within a component, and is that component that listens for "valueChange". The way to get the selected item is:

Item itemSelected = component.getContainerDataSource().getItem(component.getValue());
//get for example "id" property
Integer id = (Integer) itemSelected.getItemProperty("id").getValue();

Otherwise, are you just using a container alone? If so can you be more specific? because it's not a "standard" way. Regards

MarcelloGarini
  • 599
  • 2
  • 13
  • Yes, I am using similar way now. My object is DbLookup extends Label. The container, row (itemId) and column (propertyId) = keyvalue I need, are passed in constructor. It gets the content from another table, joining it using the keyvalue. "valueChange" is refreshing it. I will mark the anwer as useful. But still I am sad there is no way how to determine what has been changed exactly. Or yes? – Hink Oct 14 '14 at 13:22
  • The above lines gave you exactly the current selected item, which is the "new value" of the valueChangeEvent fired by the component. Now this makes sense for components which have a container (a store of data) that, in some way, can change their own value (example: ComboBox). I don't quite understand what are you trying to do with your extended Label, but if you care to explain I can elaborate more. – MarcelloGarini Oct 14 '14 at 15:37
0

It's not very efficient, but it works:

container.addValueChangeListener(new ValueChangeListener() {
    private static final long serialVersionUID = 1L;

    @Override
    public void valueChange(ValueChangeEvent event) {
        for (Object itemId : container.getItemIds()) {
            for (Object propId : container.getContainerPropertyIds()) {
                Property<?> p = container.getContainerProperty(itemId, propId);
                Object o = ((EventObject) event).getSource();
                if (p.equals(o)) {
                    System.out.println("itemId: " + itemId);
                    System.out.println("propertyId: " + propId);
                    return;
                }
            }
        }
    }
});

The event is a PropertyValueChangeEvent and the getSource() method returns IndexedContainerProperty. The problem is that those are private classes. The IndexedContainerProperty contains the itemId and the propertyId too.

Krayo
  • 2,492
  • 4
  • 27
  • 45
  • Yes, this the worst way how to do it, but is there any better way? I'd like to know what information is given in the parameter "event". F.e. some value of Integer has changed to 5. I dont know what row, I dont know what column, I dont know previous value. Such event had to be invented by real head.

    – Hink Oct 14 '14 at 21:18