2017-08-13 52 views
2

我創建了MapProperty以從數據庫讀取信息,如下所示。將數據庫讀取到MapProperty Javafx

命令運行正常使用地圖,ArrayList的正常,但錯誤MapProperty.I希望委員爲的ListProperty類型,所以我可以綁定它來控制

public MapProperty<String, ListProperty<String>> mapTaxonomy() { 
     MapProperty<String, ListProperty<String>> mapTaxonomy = new SimpleMapProperty<>(); 
     try { 
      preparedStatement = connection.prepareStatement(""); 
      resultSet = preparedStatement.executeQuery(); 
      while (resultSet.next()) { 
       String taxonomy = resultSet.getString("Taxonomy"); 
       ListProperty<String> memberSelector = mapTaxonomy.get(taxonomy); 
       if (memberSelector == null) { 
        memberSelector = new SimpleListProperty<>(); 
        mapTaxonomy.put(taxonomy, memberSelector); 
       } 
       memberSelector.add(resultSet.getString("Selector")); 
      } 
     } catch (SQLException ex) { 
      Logger.getLogger(ParserService.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return mapTaxonomy; 
    } 

@Override 
    public void initialize(URL location, ResourceBundle resources) { 
     MapProperty<String, ListProperty<String>> mapTaxonomy = mapTaxonomy(); 
    } 

我運行了以下錯誤語句後,請幫我

Caused by: java.lang.UnsupportedOperationException 
    at java.util.AbstractMap.put(AbstractMap.java:209) 
    at javafx.beans.binding.MapExpression.put(MapExpression.java:262) 
    at touya.akira.storages.database.table.parser.ParserService.mapTaxonomy(ParserService.java:70) 
    at touya.akira.parser.styles.fixed.method.pagination.PaginationPresenter.initialize(PaginationPresenter.java:64) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 
    ... 67 more 

回答

1

一個SimpleMapProperty的默認值是空不可修改地圖。當您嘗試修改它時引發異常。指定一個可修改的ObservableMap作爲初始值來解決此問題。

MapProperty<String, ListProperty<String>> mapTaxonomy = new SimpleMapProperty<>(FXCollections.observableHashMap()); 
+0

感謝@fabian你的幫助我成功了,我必須要找到的一些Oracle文檔「一SimpleMapProperty的默認值是一個空的不可修改的」我不上它。如果可能的 找到教程,請幫助我的文檔,我指更 https://docs.oracle.com/javafx/2/api/javafx/beans/property/SimpleMapProperty.html –

+1

@KiênĐịnh似乎沒有要在javadoc中的任何指示;我看着源代碼... – fabian