我有一个类BillingController它包含tableview.i想要更新一个qty单元格也会自动更新一个数量的cell.how来更新tableview中的一个数量单元格 这是代码:
colSno.setCellValueFactory(new PropertyValueFactory<BillingModel,String>("s_no"));
colSno.setPrefWidth( 100 );
colItem.setCellValueFactory(
new PropertyValueFactory<BillingModel,String>("item_name")
);
colItem.setPrefWidth( 250 );
colQty.setCellValueFactory(
new PropertyValueFactory<BillingModel,String>("quantity")
);
colQty.setPrefWidth( 100 );
tableBill.setEditable( true );
javafx.util.Callback<TableColumn, TableCell> cellFactory =
new javafx.util.Callback<TableColumn, TableCell>() {
@Override
public TableCell call(TableColumn p) {
return new EditingCell();
}
};
colQty.setCellFactory(cellFactory);
colRate.setCellValueFactory(
new PropertyValueFactory<BillingModel,String>("rate")
);
colRate.setPrefWidth( 100 );
colAmount.setCellValueFactory(
new PropertyValueFactory<BillingModel,String>("amount")
);
colAmount.setPrefWidth( 120 );
colAmount.setCellFactory(new javafx.util.Callback<TableColumn<BillingModel,String>, TableCell<BillingModel,String>>()
{
@Override
public TableCell<BillingModel, String> call(
TableColumn<BillingModel, String> param)
{
return new TableCell<BillingModel, String>()
{
@Override
protected void updateItem(String item, boolean empty)
{
if (!empty)
{
int currentIndex = indexProperty()
.getValue() < 0 ? 0
: indexProperty().getValue();
String type = param
.getTableView().getItems()
.get(currentIndex).quantity;
BillingModel billingModel = modelObservableList.get(currentIndex);
System.out.println("chenged item---->"+billingModel.getQuantity());
setText(item);
}
}
@Override
protected boolean isItemChanged(String oldItem, String newItem) {
return super.isItemChanged(oldItem, newItem);
}
};
}
});
并编辑表格单元格
public class EditingCell extends TableCell<BillingModel, String> {
private TextField textField;
public EditingCell() {
}
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Platform.runLater(new Runnable() {
@Override
public void run() {
textField.requestFocus();
textField.selectAll();
}
});
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(getString());
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
int index = getTableRow().getIndex();
BillingModel billingModel = getTableView().getItems().get(index);
billingModel.setQuantity(textField.getText());
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
} else if (t.getCode() == KeyCode.TAB) {
commitEdit(textField.getText());
TableColumn nextColumn = getNextColumn(!t.isShiftDown());
if (nextColumn != null) {
getTableView().edit(getTableRow().getIndex(), nextColumn);
}
}
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (!newValue && textField != null) {
commitEdit(textField.getText());
}
}
});
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
/**
*
* @param forward true gets the column to the right, false the column to the left of the current column
* @return
*/
private TableColumn<BillingModel, ?> getNextColumn(boolean forward) {
List<TableColumn<BillingModel, ?>> columns = new ArrayList<>();
for (TableColumn<BillingModel, ?> column : getTableView().getColumns()) {
columns.addAll(getLeaves(column));
}
//There is no other column that supports editing.
if (columns.size() < 2) {
return null;
}
int currentIndex = columns.indexOf(getTableColumn());
int nextIndex = currentIndex;
if (forward) {
nextIndex++;
if (nextIndex > columns.size() - 1) {
nextIndex = 0;
}
} else {
nextIndex--;
if (nextIndex < 0) {
nextIndex = columns.size() - 1;
}
}
return columns.get(nextIndex);
}
private List<TableColumn<BillingModel, ?>> getLeaves(TableColumn<BillingModel, ?> root) {
List<TableColumn<BillingModel, ?>> columns = new ArrayList<>();
if (root.getColumns().isEmpty()) {
//We only want the leaves that are editable.
if (root.isEditable()) {
columns.add(root);
}
return columns;
} else {
for (TableColumn<BillingModel, ?> column : root.getColumns()) {
columns.addAll(getLeaves(column));
}
return columns;
}
}
} 如何基于qty单元更新另一个单元格? 对此有任何想法将非常感激。 :d
谢谢,Priya