7

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

如果我忽略该return SimpleObjectProperty行,则for循环将遍历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,.........);
4

2 回答 2

12
  1. CellFactory.Callback.call()只创建一个单元格,而不是循环中的所有单元格
  2. 使用returnfrom 循环会中断循环执行。

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

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();
    }
}
于 2013-09-04T17:55:56.207 回答
3

谢尔盖·格里涅夫;我找到了一个解决方案,这个问题的通用解决方案

public class TableCassaController<K,V> extends TableView<Map.Entry<K,V>> implements Initializable {
@FXML   private TableColumn<K, V> column1;
@FXML   private TableColumn<K, V> column2;


public TableCassaController(ObservableMap<K,V> map, String col1Name, String col2Name) {
    System.out.println("Costruttore table");
    TableColumn<Map.Entry<K, V>, K> column1 = new TableColumn<>(col1Name);
    column1.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<K, V>, K>, ObservableValue<K>>() {

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

    TableColumn<Map.Entry<K, V>, V> column2 = new TableColumn<>(col2Name);
    column2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<K, V>, V>, ObservableValue<V>>() {

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

    ObservableList<Map.Entry<K, V>> items = FXCollections.observableArrayList(map.entrySet());

    this.setItems(items);
    this.getColumns().setAll(column1, column2);

}

很感谢!!!:-)

于 2017-04-11T07:28:02.033 回答