Java 瓦丁将表格内容放入地图

Java 瓦丁将表格内容放入地图,java,vaadin,Java,Vaadin,我对瓦丁有点陌生,已经看过他们的书了。我的问题是关于一个可编辑的表,以及当它被更改时如何从中获取值 所以基本上我有一个简单的表,我用一个HashMap填充它。因此,“编辑”按钮使字段可编辑,而“添加行”按钮则添加一行。实现非常简单。但我没有管理的是更新按钮,因为当我添加一行或编辑某个字段时,我想将所有更改的键和值成对地重新放入hashmap中,并用它做一些事情 我知道Property.ValueChangeListener提供了所有已更改的单元格,但我需要的是关于整行的信息,而不是单个单元格。例

我对瓦丁有点陌生,已经看过他们的书了。我的问题是关于一个可编辑的表,以及当它被更改时如何从中获取值

所以基本上我有一个简单的表,我用一个HashMap填充它。因此,“编辑”按钮使字段可编辑,而“添加行”按钮则添加一行。实现非常简单。但我没有管理的是更新按钮,因为当我添加一行或编辑某个字段时,我想将所有更改的键和值成对地重新放入hashmap中,并用它做一些事情

我知道
Property.ValueChangeListener
提供了所有已更改的单元格,但我需要的是关于整行的信息,而不是单个单元格。例如,当
val2
更改为
val22
时,我想根据它更新hashmap。因此,我还需要获取
key2
。简单地说,我想得到整条线,当这条线发生变化时。有什么想法吗

我的UI如下所示,我使用的是Vaadin 6:

对于那些可能需要这种东西的人,我找到了一个解决办法:

// I needed to fill the table with an IndexedContainer
final IndexedContainer container = new IndexedContainer();
// table headers
container.addContainerProperty("Metadata Key", String.class, null);
container.addContainerProperty("Metadata Value", String.class, null);

// Now fill the container with my hashmap (objectMetadatas) and at the end we will add the container to the table
int i = 0;
for (String k : objectMetadatas.keySet()) {
    Integer rowId = new Integer(i);
    container.addItem(rowId);
    container.getContainerProperty(rowId, "Metadata Key").setValue(k);
    container.getContainerProperty(rowId, "Metadata Value").setValue(objectMetadatas.get(k));
    i++;
}

// then added a ValueChangeListener to the container
container.addListener(new Property.ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
         Property p = event.getProperty(); // not necessary
             System.out.println(p);        // not necessary
    }
});

// add the the button to update the table and get the changed values into your hashmap
buttonUpdate.addListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        Map<String, String> map = new HashMap<String,String>();
        Collection i = tableMetadata.getContainerDataSource().getItemIds();
        for (int x = 0; x < i.size(); x++) {
            // Items in Vaadin represent rows, since I have two columns
            // i have only two values in my row as following: "row1 row2"
            // if you have four columns on your table 
            // your item will look like this: "row1 row2 row3 row4"
            Item it=myTable.getContainerDataSource().getItem(x);
            // thats why I am splitting it
            String[] row= it.toString().split(" ");
            map.put(row[0], row[1]);
        }
        // Now all changed values are in your map! 
        // Do sth with that map
    }
});

// Finally do not forget to add that container to your table
tableMetadata.setContainerDataSource(container);
//我需要用IndexedContainer填充表
最终IndexedContainer容器=新IndexedContainer();
//表格标题
container.addContainerProperty(“元数据键”,String.class,null);
container.addContainerProperty(“元数据值”,String.class,null);
//现在用我的hashmap(objectMetadatas)填充容器,最后将容器添加到表中
int i=0;
for(字符串k:objectMetadatas.keySet()){
整数rowId=新整数(i);
container.addItem(rowId);
container.getContainerProperty(rowId,“元数据键”).setValue(k);
getContainerProperty(rowId,“元数据值”).setValue(objectMetadatas.get(k));
i++;
}
//然后将ValueChangeListener添加到容器中
container.addListener(新属性.ValueChangeListener(){
public void valueChange(valuechangevent事件){
属性p=event.getProperty();//不需要
System.out.println(p);//不需要
}
});
//添加按钮以更新表并将更改后的值获取到hashmap中
buttonUpdate.addListener(新建Button.ClickListener(){
公共作废按钮单击(单击事件){
Map Map=newhashmap();
集合i=tableMetadata.getContainerDataSource().GetItemId();
对于(int x=0;x

就这些!希望它能对某人有所帮助!如果您找到更好的方法,请发布。

对于那些可能需要这种东西的人,我找到了一个解决方法:

// I needed to fill the table with an IndexedContainer
final IndexedContainer container = new IndexedContainer();
// table headers
container.addContainerProperty("Metadata Key", String.class, null);
container.addContainerProperty("Metadata Value", String.class, null);

// Now fill the container with my hashmap (objectMetadatas) and at the end we will add the container to the table
int i = 0;
for (String k : objectMetadatas.keySet()) {
    Integer rowId = new Integer(i);
    container.addItem(rowId);
    container.getContainerProperty(rowId, "Metadata Key").setValue(k);
    container.getContainerProperty(rowId, "Metadata Value").setValue(objectMetadatas.get(k));
    i++;
}

// then added a ValueChangeListener to the container
container.addListener(new Property.ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
         Property p = event.getProperty(); // not necessary
             System.out.println(p);        // not necessary
    }
});

// add the the button to update the table and get the changed values into your hashmap
buttonUpdate.addListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        Map<String, String> map = new HashMap<String,String>();
        Collection i = tableMetadata.getContainerDataSource().getItemIds();
        for (int x = 0; x < i.size(); x++) {
            // Items in Vaadin represent rows, since I have two columns
            // i have only two values in my row as following: "row1 row2"
            // if you have four columns on your table 
            // your item will look like this: "row1 row2 row3 row4"
            Item it=myTable.getContainerDataSource().getItem(x);
            // thats why I am splitting it
            String[] row= it.toString().split(" ");
            map.put(row[0], row[1]);
        }
        // Now all changed values are in your map! 
        // Do sth with that map
    }
});

// Finally do not forget to add that container to your table
tableMetadata.setContainerDataSource(container);
//我需要用IndexedContainer填充表
最终IndexedContainer容器=新IndexedContainer();
//表格标题
container.addContainerProperty(“元数据键”,String.class,null);
container.addContainerProperty(“元数据值”,String.class,null);
//现在用我的hashmap(objectMetadatas)填充容器,最后将容器添加到表中
int i=0;
for(字符串k:objectMetadatas.keySet()){
整数rowId=新整数(i);
container.addItem(rowId);
container.getContainerProperty(rowId,“元数据键”).setValue(k);
getContainerProperty(rowId,“元数据值”).setValue(objectMetadatas.get(k));
i++;
}
//然后将ValueChangeListener添加到容器中
container.addListener(新属性.ValueChangeListener(){
public void valueChange(valuechangevent事件){
属性p=event.getProperty();//不需要
System.out.println(p);//不需要
}
});
//添加按钮以更新表并将更改后的值获取到hashmap中
buttonUpdate.addListener(新建Button.ClickListener(){
公共作废按钮单击(单击事件){
Map Map=newhashmap();
集合i=tableMetadata.getContainerDataSource().GetItemId();
对于(int x=0;x

就这些!希望它能对某人有所帮助!如果您找到更好的方法,请将其发布。

您可以看看我编写的一个附加组件,它充当
HashMap
和Vaadin的
容器之间的适配器。这远非完美,但至少应该让你开始。请随时提出您对它的任何问题。谢谢,我会看一看。您可以看一看,这是我编写的一个附加组件,用作
HashMap
和Vaadin的
容器之间的适配器。这远非完美,但至少应该让你开始。请随时提出您的任何问题。谢谢,