【问题标题】:JavaFX: Populate TableView with an ObservableMap that has a custom class for its valuesJavaFX:使用具有自定义类的 ObservableMap 填充 TableView
【发布时间】:2016-11-24 01:49:33
【问题描述】:

我想在TableView 中显示以下 ObservableMap:

private ObservableMap<String, Shape> myShapes = FXCollections.observableHashMap();

其中Shape定义如下:

public class Shape {

private StringProperty areaFormula = new SimpleStringProperty();
private IntegerProperty numSides = new SimpleIntegerProperty();

public Shape(String areaFormula, int numSides)
{
    this.areaFormula.set(areaFormula);
    this.numSides.set(numSides);
}

public String getAreaFormula() { return areaFormula.get(); }

public void setAreaFormula(String areaFormula) { this.areaFormula.set(areaFormula); }

public StringProperty areaFormulaProperty() { return this.areaFormula; }

public int getNumSides() { return numSides.get(); }

public void setNumSides(int sides) { this.numSides.set(sides); }

public IntegerProperty numSidesProperty() { return this.numSides; }
}

我希望 TableView 的第一列是 Map Key(称为 key),第二列是 myShapes.get(key).getAreaFormula(),第三列是 myShapes.get(key).getnumSides()。我希望 TableView 在地图更改时自动更新。

如果可能的话,让用户可以编辑TableView 的第二列和第三列也非常好,这些编辑会在地图中更新。

我已经向 stackoverflow 询问了关于使用 ObservableMap 填充 TableView 以便它会在此处更新更改: Populating a TableView with a HashMap that will update when HashMap changes 。但是,正如您在讨论中看到的那样,它不像我在这里那样处理自定义类。

到目前为止,这是我的解决方案:

        ObservableMap<String, Shape> map = FXCollections.observableHashMap();

        ObservableList<String> keys = FXCollections.observableArrayList();

        map.addListener((MapChangeListener.Change<? extends String, ? extends Shape> change) -> {
            boolean removed = change.wasRemoved();
            if (removed != change.wasAdded()) {
                // no put for existing key
                if (removed) {
                    keys.remove(change.getKey());
                } else {
                    keys.add(change.getKey());
                }
            }
        });

        map.put("square", new Shape("L^2", 4));
        map.put("rectangle", new Shape("LW", 4));
        map.put("triangle", new Shape("0.5HB", 3));

        final TableView<String> table = new TableView<>(keys);

        TableColumn<String, String> column1 = new TableColumn<>("Key");

        column1.setCellValueFactory(cd -> Bindings.createStringBinding(() -> cd.getValue()));

        TableColumn<String, String> column2 = new TableColumn<>("Value");

        column2.setCellValueFactory( ... );

        table.getColumns().setAll(column1, column2);

我卡住的地方是倒数第二行column2.setCellValueFactory( ... )。我希望column2 显示Shape 的getAreaFormula() SimpleStringProperty,但我不知道如何设置CellValueFactory 这样做。

感谢您的帮助。

【问题讨论】:

    标签: java javafx tableview


    【解决方案1】:

    如果您创建一个不可变的键值对列表而不是简单的键列表,这是最简单的。这大大降低了您需要使用的侦听器的复杂性,因为MapChangeListener 将始终替换一个项目,如果更新了值并且您不需要在cellValueFactorys 中将侦听器添加到Map:

    键值对类

    public final class MapEntry<K, V> {
    
        private final K key;
        private final V value;
    
        public MapEntry(K key, V value) {
            this.key = key;
            this.value = value;
        }
    
        @Override
        public boolean equals(Object obj) {
            // check equality only based on keys
            if (obj instanceof MapEntry) {
                MapEntry<?, ?> other = (MapEntry<?, ?>) obj;
                return Objects.equals(key, other.key);
            } else {
                return false;
            }
        }
    
        public K getKey() {
            return key;
        }
    
        public V getValue() {
            return value;
        }
    
    }
    

    表创建

    ObservableMap<String, Shape> map = FXCollections.observableHashMap();
    
    ObservableList<MapEntry<String, Shape>> entries = FXCollections.observableArrayList();
    
    map.addListener((MapChangeListener.Change<? extends String, ? extends Shape> change) -> {
        boolean removed = change.wasRemoved();
        if (removed != change.wasAdded()) {
            if (removed) {
                // no put for existing key
                // remove pair completely
                entries.remove(new MapEntry<>(change.getKey(), (Shape) null));
            } else {
                // add new entry
                entries.add(new MapEntry<>(change.getKey(), change.getValueAdded()));
            }
        } else {
            // replace existing entry
            MapEntry<String, Shape> entry = new MapEntry<>(change.getKey(), change.getValueAdded());
    
            int index = entries.indexOf(entry);
            entries.set(index, entry);
        }
    });
    
    map.put("one", new Shape("a", 1));
    map.put("two", new Shape("b", 2));
    map.put("three", new Shape("c", 3));
    
    final TableView<MapEntry<String, Shape>> table = new TableView<>(entries);
    TableColumn<MapEntry<String, Shape>, String> column1 = new TableColumn<>("Key");
    
    // display item value (= constant)
    column1.setCellValueFactory(cd -> Bindings.createStringBinding(() -> cd.getValue().getKey()));
    
    TableColumn<MapEntry<String, Shape>, String> column2 = new TableColumn<>("formula");
    column2.setCellValueFactory(cd -> cd.getValue().getValue().areaFormulaProperty());
    
    TableColumn<MapEntry<String, Shape>, Number> column3 = new TableColumn<>("sides");
    column3.setCellValueFactory(cd -> cd.getValue().getValue().numSidesProperty());
    
    table.getColumns().setAll(column1, column2, column3);
    

    【讨论】:

    • entries.indexOf() 和 index.remove() 是 O(n) 操作。您需要 O(1) 解决方案,尤其是当您的表很大或经常更新时。
    • 我在 tableview 中有一些问题。请检查一下stackoverflow.com/questions/67368501/…
    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2016-02-16
    • 2021-07-26
    • 2014-10-08
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多