JavaFX行未更新

时间:2013-05-25 15:40:49

标签: javafx

有一个非常类似的问题,但我的有点不同。我正在使用属性和可观察列表来更改它,它不会更新。 原始问题是here

所以当我在表之间传输行时,像这样:

Row transferring

第一行会出现,但是当添加多行时会导致第一行之后的行不更新,如下所示:

Row transferring with the bug

当我在柱子周围移动时,它们才会重新出现。

    //Loot identification
    TableColumn lootIdentCol = new TableColumn<>("Identification");

    TableColumn<ItemDef, Integer> lootIDCol = new TableColumn<>("ID");
    lootIDCol.setCellValueFactory(
            new PropertyValueFactory<ItemDef, Integer>("id"));

    TableColumn<ItemDef, String> lootNameCol = new TableColumn<>("Name");
    lootNameCol.setCellValueFactory(
            new PropertyValueFactory<ItemDef, String>("name"));

    lootIdentCol.getColumns().addAll(lootNameCol, lootIDCol);

    //Loot price
    TableColumn<ItemDef, Integer> lootPriceCol = new TableColumn<>("Price");
    lootPriceCol.setCellValueFactory(
            new PropertyValueFactory<ItemDef, Integer>("price"));

    //To loot items table
    toLootItemsTableView.getColumns().addAll(lootIdentCol, lootPriceCol);
    grid.add(toLootItemsTableView, 0, 1);

    //Lootable items table
    lootableItemsTableView.getColumns().addAll(lootIdentCol, lootPriceCol);
    grid.add(lootableItemsTableView, 2, 1);

    toLootItemsTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    lootableItemsTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    lootableItemsTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    toLootItemsTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

    lootableTableList.add(new ItemDef("Ab", 141, false, false));
    lootableTableList.add(new ItemDef("Ac", 25, false, false));
    lootableTableList.add(new ItemDef("AD", 262, false, false));

    AddRemoveButtons<ItemDef> addRemoveLootButtons = new AddRemoveButtons<>(
            lootableTableList, lootableItemsTableView.getSelectionModel(),
            toLootTableList, toLootItemsTableView.getSelectionModel()
    );

AddRemoveButtons的代码:

private final ObservableList<E> fromList;
private final ObservableList<E> toList;

public AddRemoveButtons(final ObservableList<E> fromList, final SelectionModel<E> from,
                        final ObservableList<E> toList, final SelectionModel<E> to) {
    this.fromList = fromList;
    this.toList = toList;

    setAlignment(Pos.CENTER);
    setPadding(new Insets(5, 5, 5, 5));
    setSpacing(15);

    ObservableList<Node> children = getChildren();

    Button moveInto = new Button("Add");
    moveInto.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if (from instanceof MultipleSelectionModel) {
                MultipleSelectionModel<E> multipleFrom = (MultipleSelectionModel<E>) from;
                ObservableList<Integer> selectedIndices = multipleFrom.getSelectedIndices();
                for (int i : selectedIndices)
                    transfer(i, true);
            } else
                transfer(from.getSelectedIndex(), true);
        }
    });

    Button delete = new Button("Del");
    delete.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if (to instanceof MultipleSelectionModel) {
                MultipleSelectionModel<E> multipleFrom = (MultipleSelectionModel<E>) to;
                ObservableList<Integer> selectedIndices = multipleFrom.getSelectedIndices();
                for (int i : selectedIndices)
                    transfer(i, false);
            } else
                transfer(to.getSelectedIndex(), false);
        }
    });

    children.addAll(moveInto, delete);
}

private void transfer(int index, boolean forward) {
    if (forward)
        toList.add(fromList.remove(index));
    else
        fromList.add(toList.remove(index));
}

ItemDef,它实现了Identifiable,Serializable,Comparable:

private final String  name;
private final int     id;
private final boolean members;
private final boolean stackable;
private       int     price;

public ItemDef(JSONObject jsonObject) {
    this(
            (String) jsonObject.get("name"),
            Integer.parseInt((String) jsonObject.get("id")),
            Boolean.parseBoolean((String) jsonObject.get("members")),
            Boolean.parseBoolean((String) jsonObject.get("stackable"))
    );
}

public ItemDef(String name, int id, boolean members, boolean stackable) {
    this.name = name;
    this.id = id;
    this.members = members;
    this.stackable = stackable;
    price = -1;
}

public String getName() {
    return name;
}

@Override
public int getId() {
    return id;
}

public boolean isMembers() {
    return members;
}

public boolean isStackable() {
    return stackable;
}

public int getPrice() {
    return price != -1 ? price : updatePrice();
}

//Other methods not relevant

2 个答案:

答案 0 :(得分:2)

弄清楚为什么一直这样做。

您不能在多个表上引用相同的TableColumn。

答案 1 :(得分:0)

如果您希望在多个表中更新数据,而不是在列之间共享数据集,则不应在多个表中共享列。