使用 ObservableMap JavaFX 填充 TableView [英] Populate TableView with ObservableMap JavaFX

查看:26
本文介绍了使用 ObservableMap JavaFX 填充 TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 ObservableMap 来填充 TableView ?我使用ObservableMap而不是ObservableList,因为我需要经常添加和删除,所以我需要最小化成本.

I wanted to know if it is possible to use a ObservableMap to populate a TableView ? I use ObservableMap instead of ObservableList because I need to add and delete often, so I need to minimize the cost.

我的 hashMap 使用 BigInteger 作为键字段,使用具有许多属性的类型作为值字段.在我的 tableView 中,我只想显示每个属性的列的值.我希望这很清楚

My hashMap use an BigInteger as key field and a type with many properties as value field. In my tableView I just want to display the values with a column per properties. I hope that is clear

谢谢

推荐答案

我一直在尝试这样做.我猜这个帖子很旧,但我在网上的任何地方都没有看到任何答案.这些示例对列使用映射键,然后对每一行使用映射列表.我想将行视为键及其关联的值.这是一个很长的例子.

I've been trying to do this. I guess the post is old but I don't see any answers anywhere on the net. The examples use the map key for columns and then a list of maps for every row. I'd like to see the rows as keys and their associated values. It's a long example.

package tablemap;

import static java.lang.Math.random;
import java.util.Map;
import java.util.TreeMap;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableMap extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();
        Map<String,LineItem> mapData = new TreeMap<>();
        for (int i = 0; i < 3; i++)
            mapData.put(String.valueOf(random()), new LineItem(String.valueOf(i),"i"));

        ObservableList<Map.Entry<String,LineItem>> listData = 
                FXCollections.observableArrayList(mapData.entrySet());
        TableView<Map.Entry<String,LineItem>> tv = new TableView(listData);

        TableColumn<Map.Entry<String,LineItem>,String> keyCol = new TableColumn("Key");
        keyCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getKey()));

        TableColumn<Map.Entry<String,LineItem>,String> lineNoCol = new TableColumn("Line No");
        lineNoCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getValue().getLineNo()));

        TableColumn<Map.Entry<String,LineItem>,String> descCol = new TableColumn("Desc");
        descCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getValue().getDesc()));

        descCol.setCellFactory(TextFieldTableCell.forTableColumn());

        descCol.setOnEditCommit((CellEditEvent<Map.Entry<String,LineItem>, String> t) -> {
             t.getTableView().getItems().get(t.getTablePosition().getRow())
                     .getValue().setDesc(t.getNewValue());
        });

        tv.getColumns().addAll(keyCol,lineNoCol, descCol);
        tv.setEditable(true);
        tv.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        Button btnOut = new Button("out");
        btnOut.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                for (Map.Entry<String,LineItem> me : mapData.entrySet()){
                    System.out.println("key "+me.getKey()+" entry "+me.getValue().toCSVString());
                }
                for (Map.Entry<String,LineItem> me : listData){
                    System.out.println("key "+me.getKey()+" entry "+me.getValue().toCSVString());
                }
            }
        });

        root.getChildren().addAll(tv,btnOut);
        Scene scene = new Scene(root, 300, 200);

        primaryStage.setTitle("Map Table Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

和 LineItem 类代码

And the LineItem Class Code

package tablemap;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/* LineItem class */

public class LineItem {
    private final StringProperty lineNo = new SimpleStringProperty();
    private final StringProperty desc = new SimpleStringProperty();

    public LineItem(String ln, String dsc) {
        lineNo.set(ln); desc.set(dsc);
    }

    public String getLineNo() {return (lineNo.getValue() != null) ?lineNo.get():"";}
    public void setLineNo(String lineNo) {this.lineNo.set(lineNo);}
    public StringProperty lineNoProperty() {return lineNo;}

    public String getDesc() {return (desc.getValue() != null) ?desc.get():"";}
    public void setDesc(String desc) {this.desc.set(desc);}
    public StringProperty descProperty() {return desc;}

    public String toCSVString(){
        return lineNo.getValueSafe()+","+
                desc.getValueSafe()+"
"; 
    }
}

您可以在编辑数据并单击后看到列表中的更改反映在地图中.我仍然需要检查其他方式并处理插入和删除,但这应该不难.

You can see after editing data and clicking out that changes in the list are reflected in the map. I still have to check the other way and handle insertions and deletions but that shouldn't be to hard.

这篇关于使用 ObservableMap JavaFX 填充 TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆