将 hashmap 与 tableview (JavaFX) 绑定 [英] Binding hashmap with tableview (JavaFX)
本文介绍了将 hashmap 与 tableview (JavaFX) 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 JavaFX Tableview
中显示 HashMap
内容.请在下面找到我用来将 HashMap
内容设置到表列中的代码.我遇到的问题是它只显示一行.for
循环只迭代了 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
.
如果我忽略 return 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,.........);
推荐答案
CellFactory.Callback.call()
只创建一个单元格,而不是循环中的所有单元格- 在循环中使用
return
会中断循环执行.
CellFactory.Callback.call()
creates just one cell, not all cells in a loop- 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();
}
}
这篇关于将 hashmap 与 tableview (JavaFX) 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文