2016-09-08 8 views
0

HERESに私の次のコード...列に列を追加するにはどうすればよいですか?

TableColumn tc = new TableColumn(); 
    TableColumn[] tc2 = new TableColumn[10]; 
    for(int i=0; i<5, i++){ 
     tc.getColumns().add(tc2[i]); 
     } 

と私はセルを編集するための方法をコミット上書きしてみてください。

public void commit(Object val) { 

    // Get the table 
    TableView<MainTable> t = this.getTableView(); 

    // Get the selected row/column 

    MainTable selectedRow = t.getItems().get(this.getTableRow().getIndex());  
    TableColumn<MainTable, ?> selectedColumn = t.getColumns().get(t.getColumns().indexOf(this.getTableColumn())); 

    // Get current property name 
    String propertyName = ((PropertyValueFactory) selectedColumn.getCellValueFactory()).getProperty(); 

    // Create a method name conforming to java standards (setProperty) 
    propertyName = ("" + propertyName.charAt(0)).toUpperCase() + propertyName.substring(1); 

    // Try to run the update 
    try { 

     // Type specific checks - could be done inside each setProperty() method 
     if(val instanceof Double) { 
      Method method = selectedRow.getClass().getMethod("set" + propertyName, double.class); 
      method.invoke(selectedRow, (double) val); 
     } 
     if(val instanceof String) { 
      Method method = selectedRow.getClass().getMethod("set" + propertyName, String.class); 
      method.invoke(selectedRow, (String) val); 
     } 
     if(val instanceof Integer) { 
      Method method = selectedRow.getClass().getMethod("set" + propertyName, int.class); 
      method.invoke(selectedRow, (int) val); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // CommitEdit for good luck 
    commitEdit((String) val); 
} 

コンソールビューでArrayIndexOutofBoundsExceptionが発生しました。

私の質問は どのように私は他の列を追加getcolumnsを選択することができますか?私はこのコードを変更する必要があると思い

TableColumn<MainTable, ?> selectedColumn = t.getColumns().get(t.getColumns().indexOf(this.getTableColumn())); 

... 誰もがアイデアを得ました?

答えて

2

ネストされた列はTableView.columnsリストの一部ではありません。代わりに見出すこと、あなただけの列自体が必要な場合は

TableColumn<MainTable, ?> selectedColumn = this.getTableColumn(); 
TableColumn<MainTable, ?> c = selectedColumn; 
while ((c = selectedColumn.getParentColumn()) != null) { 
    selectedColumn = c; 
} 

、単にthis.getTableColumn()使用:あなたが対応するTableView列が必要な場合は、parentColumnせずに列に到達するまで

は、単に階層を上がりますcolumnsリスト内の列のインデックスを検索し、同じリスト内のそのインデックスにアクセスします。 (私は後者は何が必要であると思います。)アイテムのクラスのPropertyValueFactoryを返すプロパティ場合

さらに、あなたが代わりにリフレクションを使用しての値を設定するには、このプロパティを使用することもできます。

ObservableValue obs = selectedColumn.getCellObservableValue(this.getIndex()); 
if (obs instanceof WritableValue) { 
    ((WritableValue) obs).setValue(val); 
} else { 
    // reflecitive approach 
} 

さらに、ネストされた列としてnullを追加するべきではありませんが、あなたはここでそれをやっている:

TableColumn[] tc2 = new TableColumn[10]; 
for(int i=0; i<5, i++){ 
    tc.getColumns().add(tc2[i]); 
} 
+0

をあなたは素晴らしいです!!!!!!どうもありがとう !!! –

関連する問題