2016-09-03 23 views
0

我在这种情况下我在表格单元格中的DatePicker编辑状态:JavaFX的:更改基于另一个细胞

我有一个表“访问”有一系列的列。 表格是可编辑的,您可以通过按钮添加更多空行。

其中一列取决于另一列。 主列有它自己的cellFactory来呈现组合框。 辅助列有其自己的cellFactory来创建日期选择器。

现在,该表的二次电池只有在主塔的单元中,在不同的国家“一无所有”

一切正常,只是我需要点击组合框的状态的一个改变每当辅助小区在状态变为“无”时更新它。

为了弥补,我在主列的setOnEditCommit中使用了table.refresh()函数。但是,如果您使用此功能并且单元处于“无”状态,则所有辅助单元将变为无效。

我该如何解决?

这是次级柱

public class DataEditingCellVisite extends TableCell<Visite, LocalDate> { 

private DatePicker datePicker = new DatePicker(getDate()); 
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); 

public DataEditingCellVisite() { 
    datePicker.setDisable(true); 
} 


@Override 
public void startEdit() { 

    if(!this.getTableView().getItems().get(this.getTableView().getSelectionModel().getSelectedIndex()).getStato().equals("")) { 
     datePicker.setDisable(false); 
     commitEdit(datePicker.getValue()); 
    } else { 
     datePicker.setDisable(true); 
     setText(null); 
     commitEdit(null); 
    } 

    if (!isEmpty() && !datePicker.isDisable()) { 
    super.startEdit(); 
    createDatePicker(); 
    setText(null); 
    setGraphic(datePicker); 
    } 
} 

@Override 
public void cancelEdit() { 
    super.cancelEdit(); 
    if(getDate() != null) 
     setText(getDate().format(formatter)); 
    setGraphic(null); 
} 

@Override 
public void updateItem(LocalDate item, boolean empty) { 
    super.updateItem(item, empty); 

    if (empty) { 
     setText(null); 
     setGraphic(null); 
    } else { 
     if (isEditing()) { 
      if (datePicker != null) { 
       datePicker.setValue(getDate()); 
      } 
      setText(null); 
      setGraphic(datePicker); 
     } else { 
      setGraphic(null); 
      if(datePicker.isDisable()) 
       setText(null); 
      else 
       if(getDate() != null) 
        setText(getDate().format(formatter)); 
     } 
    }     
} 

@Override 
public void commitEdit(LocalDate value) { 
    super.commitEdit(value); 
     ((Visite)this.getTableRow().getItem()).setDataParto(value); 
} 

private void createDatePicker() { 
    datePicker.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2); 
    datePicker.setOnAction((e) -> { 
     if(datePicker.getValue() != null) 
      commitEdit(datePicker.getValue()); 
    }); 
    datePicker.addEventFilter(KeyEvent.KEY_PRESSED, event -> { 
     if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB) { 
      if(datePicker.getValue() != null) 
       commitEdit(datePicker.getValue()); 
     } 
    }); 
    datePicker.focusedProperty().addListener(new ChangeListener<Boolean>(){ 
     @Override 
     public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) { 
      if(!arg2) { 
       if(datePicker.getValue() != null) 
        commitEdit(datePicker.getValue()); 
      } 
     } 
    }); 
} 

private LocalDate getDate() { 
    return getItem() == null ? null : getItem(); 
} 
} 

回答

0

的电池工厂的代码,我发现,在此基础上其他解决方案 JavaFX TableView: format one cell based on the value of another in the row

的人谁需要它的解决方案:

我在主列的CommitEdit中留下了table.refresh()方法,并且以这种方式更改了辅助列的UpdateItem方法。

int currentIndex = indexProperty() 
        .getValue() < 0 ? 0 
        : indexProperty().getValue(); 
      String type = this 
        .getTableView().getItems() 
        .get(currentIndex).getStato(); 
      if (type.equals("")) { 
       if(getText() != null) { 
        setText(null); 
        commitEdit(null); 
       } 
      } else if(getDate() != null) { 
       this.setText(getDate().format(formatter)); 
      }