我在 JavaFX2.2 中使用过 tableView。我有一列,其中保存了用户单击按钮时更新的值。这些值是动态填充的,直到这部分工作正常。但是,当我向下滚动表格以查看表格中的其他值时,单元格数据会发生变化。你能建议我需要做什么来解决这个问题吗?
这是我动态填充的表格单元格的代码,并且在向下滚动表格时会发生变化。
Callback<TableColumn, TableCell> cellFactoryField = new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(final TableColumn param) {
final Button button = new Button("Select Field");
final TableCell cell = new TableCell() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
// label.setText("Here");
if (empty) {
// System.out.println("table cell inside updateitem = "+item);
// setGraphic(null);
}
else
{
}
}
};
button.setOnAction(new EventHandler<ActionEvent>() {
private CheckBoxTreeItem<String> checkBoxTreeItem;
private CheckBoxTreeItem<String> nodeFieldName;
private CheckBoxTreeItem<String> nodeFieldName2;
private CheckBoxTreeItem<String> nodeFieldName3;
private Stage stage = new Stage();
@Override public void handle(ActionEvent e)
{
CheckBoxTreeItem<String> rootItem =
new CheckBoxTreeItem<String>("Tables");
rootItem.setExpanded(true);
final TreeView tree = new TreeView(rootItem);
tree.setEditable(true);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
{
checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample Table" );//+ (i+1));
rootItem.getChildren().add(checkBoxTreeItem);
nodeFieldName = new CheckBoxTreeItem<String>("Field Name1");
nodeFieldName2 = new CheckBoxTreeItem<String>("Field Name2");
nodeFieldName3 = new CheckBoxTreeItem<String>("Field Name3");
checkBoxTreeItem.getChildren().addAll(nodeFieldName, nodeFieldName2, nodeFieldName3);
}
tree.setRoot(rootItem);
tree.setShowRoot(true);
StackPane root = new StackPane();
root.getChildren().add(tree);
Button selectButton = new Button("Select");
HBox hbox = new HBox();
hbox.getChildren().add(selectButton);
hbox.setAlignment(Pos.CENTER);
selectButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
final ArrayList<String> selectedValues = new ArrayList<String>();
// System.out.println("Selected tree items are : ");
if(checkBoxTreeItem.isSelected())
selectedValues.add(checkBoxTreeItem.getValue());
if(nodeFieldName.isSelected())
selectedValues.add(nodeFieldName.getValue());
if(nodeFieldName2.isSelected())
selectedValues.add(nodeFieldName2.getValue());
if(nodeFieldName3.isSelected())
selectedValues.add(nodeFieldName3.getValue());
stage.hide();
for(int i = 0; i<selectedValues.size();i++)
{
if(i == selectedValues.size()-1)
selectedVals += selectedValues.get(i);
else
selectedVals += selectedValues.get(i)+",";
}
fieldNameChosen = true;
if(fieldNameChosen)
cell.setGraphic(new Label(selectedVals));
else
cell.setGraphic(button);
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(root);
borderPane.setBottom(hbox);
stage.setScene(new Scene(new Group(borderPane)));
stage.show();
}
});
if(!(cell.getGraphic() instanceof Label))
cell.setGraphic(button);
return cell;
}
};
fieldName.setCellFactory(cellFactoryField);
对于另一个需要动态显示另一个表中的值的字段,我遇到了类似的问题。下面是我使用的代码。
final int k = 0;
value.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
int noOfDataCells = k;
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
{
TableColumn column = param.getTableColumn();
int size = 0;
if(tableView1.getItems()!=null)
size = ((ObservableList) tableView1.getItems().get(0)).size();
String valueFromData = "";
if(noOfDataCells<size)
{
valueFromData = String.valueOf(((ObservableList) tableView1.getItems().get(0)).get(noOfDataCells));
}
else if(noOfDataCells == size)
{
noOfDataCells = 0;
valueFromData = String.valueOf(((ObservableList) tableView1.getItems().get(0)).get(noOfDataCells));
}
else if (noOfDataCells>size)
{
valueFromData = "";
}
noOfDataCells++;
//TODO SET THE VALUE IN THE MODEL
// ((MetaTag) column.getTableView().getItems().get( // .getIndex())).setFieldName(selectedVals);
return new SimpleStringProperty(valueFromData);
}
});