Jimmy Lee
Jimmy Lee

Reputation: 53

How can i get columns added on column?

heres my code below...

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

and i try to override commit method for editing cells.

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);
}

and i got ArrayIndexOutofBoundsException on console view.

so my question is how can i select getcolumns added other column???

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

i think this code has to be changed... anyone got ideas??

Upvotes: 0

Views: 267

Answers (1)

fabian
fabian

Reputation: 82491

Nested columns are not part of the TableView.columns list.

If you need the corresponding TableView column, just go up through the hierarchy until you reach a column without a parentColumn:

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

If you just need the column itself, simply use this.getTableColumn(), instead of finding the index of the column in the columns list and then accessing that index in the same list. (I guess the latter is what you need.)

Furthermore, if PropertyValueFactory returns properties of the item class, you could use this property to set the value instead of using reflection:

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

Furthermore you shouldn't add null as a nested column, but you're doing it here:

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

Upvotes: 2

Related Questions