0

There is a very similar question to this already, but mine's a bit different. I am using properties and on observable list to change it, it won't update. Original question is here.

So when I am transferring rows between tables, like this:

Row transferring

The first row would appear, but when adding more than one would cause the ones after the first row to not update, like this:

Row transferring with the bug

They only reappear when I move around the columns though.

    //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()
    );

Code for 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 which implements 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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Muhatashim
  • 646
  • 8
  • 22
  • 1
    Does it work in [Java 8](https://jdk8.java.net/download.html)? If not, you might want to [file a bug](https://javafx-jira.kenai.com) with an [SSCCE](http://sscce.org/) – jewelsea May 25 '13 at 17:09
  • Overall it actually makes it worse, but I do get to see two items. But they're still buggy and I can't even select an item from the left table. I also added some more code if that helps anyone. If not, I'll just report it. – Muhatashim May 25 '13 at 17:36

2 Answers2

2

Figured out why it kept doing that.

You just can't have the same TableColumn being referenced on multiple tables.

Muhatashim
  • 646
  • 8
  • 22
0

You should not share columns in multiple tables if you want data to update in multiple tables share the data set between them not the columns.