将许多HashMaps绑定到TableView

时间:2015-12-22 19:30:04

标签: java javafx

我的应用程序(2,3,4甚至20)中有一些HasMap来保持更快的结构和随机的#34;访问。我需要在HashMap中显示所有TableView的值,并继续跟踪更改。我不知道如何实现这一点,所以我尝试了这个:

正如我测试的那样,在Java中,所有对象都通过引用传递。正如我想的那样,ObservableList是引用列表,HashMap也充满了引用。所以,我做了这件事:

当对象作为新条目的值添加到HashMap时,它也必须添加到ObservableList,如下所示:

class MyVeryOwnHashMap extends HashMap<String, MyObject> {

    private final ObservableList<MyObject> list;

    public MyVeryOwnHashMap(ObservableList<Object> list) {
        this.list = list;
    }

    public MyObject put(String key, MyObject value) {
        this.list.add(value); // As list was passed by reference, we have original list
        return super.put(key, value); // Adding to this hashmap also
        // value is the reference, so placed object is the same in both places
    }

    public MyObject remove(String name) {
       MyObject removed = super.remove(name);
       this.list.remove(removed); // Remove object from global list also
}
事情变得简单:

ObservableList<MyObject> gList = FXCollections.observableList();
MyVeryOwnHashMap hashMap = new MyVeryOwnHashMap(gList);

JavaFXTable.setItems(gList);

MyObject obj = new MyObject("someNameProperty", "someValueProperty");
hashMap.put("objID", obj); // Now object shown on the GUI table as new row

["...Here is some chunk of meaningless code..."]

// Try to change some things
MyObj objWillChange = hashMap.get("objID"); // Should we get a reference to the same object?
objWillChange.setValueProperty("someOtherValue"); // Internally, each property is the SimpleStringProperty object

我希望看到我的愚蠢想法的结果,但没有任何反应!在表格中仍然显示旧someValueProperty :(正如我所说,我的应用程序可以有很多这样的HashMap,我仍然不知道,我应该做什么。

1 个答案:

答案 0 :(得分:0)

我的Minimal, Complete, and Verifiable example没有使用你的javafx类,因为我没有安装lib )表明HashMap确实返回了引用,并且对象具有其值改变。

我做了一些小修改,例如将MyObj更改为匹配MyObject,在MyVeryOwnHashMap末尾插入缺少的括号,并从remove返回已删除的对象方法。显然我也创建了自己的MyObject类。

public class Test
{
    public static void main(String[] args)
    {
        ArrayList<MyObject> gList = new ArrayList<MyObject>();
        MyVeryOwnHashMap hashMap = new MyVeryOwnHashMap(gList);

        MyObject obj = new MyObject("someNameProperty", "someValueProperty");
        System.out.println("Obj:           " + obj.getValue());
        hashMap.put("objID", obj); 

        // Try to change some things
        MyObject objWillChange = hashMap.get("objID");
        objWillChange.setValueProperty("someOtherValue");

        System.out.println("=== After Change ===");
        System.out.println("Obj:           " + obj.getValue());
        System.out.println("hashMap:       " + hashMap.get("objID").getValue());
        System.out.println("objWillChange: " + objWillChange.getValue());
    }
}

class MyObject {
    String name;
    String value;

    public MyObject(String name, String value)
    {
        this.name = name;
        this.value = value;
    }

    public void setValueProperty(String value)
    {
        this.value = value;
    }

    public String getValue()
    {
        return value;
    }
}

class MyVeryOwnHashMap extends HashMap<String, MyObject> {

    private final List<MyObject> list;

    public MyVeryOwnHashMap(List<MyObject> list) {
        this.list = list;
    }

    public MyObject put(String key, MyObject value) {
        this.list.add(value);
        return super.put(key, value);
    }

    public MyObject remove(String name) {
       MyObject removed = super.remove(name);
       this.list.remove(removed);
       return removed;
    }
}

至于下一步要检查的地方,我首先要做的与我在这里做的类似,但包括你的JavaFX类。然后开始添加其他代码,直到你发现它变得混乱为止。