使用tableview绑定hashmap(JavaFX) [英] Binding hashmap with tableview (JavaFX)

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

问题描述

我想在JavaFX Tableview 中显示 HashMap 内容。请在下面找到我用于将 HashMap 内容设置到表列中的代码。我遇到的问题是它只显示一行。 循环的只迭代5次:每次获取 HashMap的第一个值

I want to display HashMap contents in a JavaFX Tableview. Please find below the code I used to set the HashMap contents into the table columns. The problem I'm having is that it's displaying only one row. The for loop is iterating only 5 times: each time it is picking up the first value of the HashMap.

如果我忽略返回SimpleObjectProperty 行, for 循环迭代 HashMap 中的所有内容。

If I ignore the return SimpleObjectProperty line, the for loop is iterating over all the content in the HashMap.

final ObservableList<Map> data = FXCollections.observableArrayList();
data.addAll(HASHMAP);

TableColumn<Map.Entry, String> nCol = new TableColumn<Map.Entry, String>("Name");
nCol.setEditable(true);
nCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Entry, String>, ObservableValue<String>>() {

 @Override
 public ObservableValue<String> call(TableColumn.CellDataFeatures<Entry, String> p) {
        Set <String> set=HASHMAP.keySet();
    for (String key:HASHMAP.keySet())
    {
           String key1= key.toString();
           return new SimpleObjectProperty<>(key.toString());
    }
         return null;

        } 

    });
  Table.setItems(data);
  Table.getColumns().setAll(nCol,.........);


推荐答案


  1. CellFactory.Callback.call()只创建一个单元格,而不是循环中的所有单元格

  2. 使用 return 来自循环中断循环执行。

  1. CellFactory.Callback.call() creates just one cell, not all cells in a loop
  2. Using return from a loop breaks loop execution.

看看下一个例子,特别是评论:

Take a look at next example, especially comments:

public class MapTableView extends Application {

    @Override
    public void start(Stage stage) {

        // sample data
        Map<String, String> map = new HashMap<>();
        map.put("one", "One");
        map.put("two", "Two");
        map.put("three", "Three");


        // use fully detailed type for Map.Entry<String, String> 
        TableColumn<Map.Entry<String, String>, String> column1 = new TableColumn<>("Key");
        column1.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<String, String>, String>, ObservableValue<String>>() {

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Map.Entry<String, String>, String> p) {
                // this callback returns property for just one cell, you can't use a loop here
                // for first column we use key
                return new SimpleStringProperty(p.getValue().getKey());
            }
        });

        TableColumn<Map.Entry<String, String>, String> column2 = new TableColumn<>("Value");
        column2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<String, String>, String>, ObservableValue<String>>() {

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Map.Entry<String, String>, String> p) {
                // for second column we use value
                return new SimpleStringProperty(p.getValue().getValue());
            }
        });

        ObservableList<Map.Entry<String, String>> items = FXCollections.observableArrayList(map.entrySet());
        final TableView<Map.Entry<String,String>> table = new TableView<>(items);

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

        Scene scene = new Scene(table, 400, 400);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

这篇关于使用tableview绑定hashmap(JavaFX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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