Weeedooo
Weeedooo

Reputation: 557

JavaFX passing data from one TableView to another

What Im trying to do is copy selected row in one TableView to another one TableView.

 public void addMeal()
{
    products2 = FXCollections.observableArrayList();
    tableProduct.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<productData>() {
        @Override
        public void changed(ObservableValue<? extends productData> observable, productData oldValue, productData newValue) {
            products2.add(new productData(newValue.getName(), newValue.getKcal(), newValue.getProtein(), newValue.getCarb(), newValue.getFat()));
        }
    });
    colProduct2.setCellValueFactory(new PropertyValueFactory<productData, String>("name"));
    colKcal2.setCellValueFactory(new PropertyValueFactory<productData, String>("kcal"));
    colProtein2.setCellValueFactory(new PropertyValueFactory<productData, String>("protein"));
    colCarbs2.setCellValueFactory(new PropertyValueFactory<productData, String>("carb"));
    colFat2.setCellValueFactory(new PropertyValueFactory<productData, String>("fat"));
    tableProduct2.setItems(products2);
}

products2 is ObservableList, tableProduct2 is TableView, colName2, etc are TableColumns

Problem is that, app is working not correctly. I have to push the button connected with this method first, then after I select row in TableView 1 i get in TableView 2. Whats more, I can click few others rows and they will be added too. Its like this button enables this selection option and what I want is to simply select row, click button, see this row in another TableView. Then I want to select another one, click button again, etc.

Next problem is that, once I click button again, it clears second TableView and from now on, he will add selected items twice, if I click button again, it clears TableView again and now it adds selected item 3 times, etc...

Upvotes: 0

Views: 2393

Answers (1)

fabian
fabian

Reputation: 82451

What is happening here is that you do not add a listener before the button is clicked. When the button is clicked the first time, this does not trigger the listener. Only later when you change the selection, the listnener will be notified. By clicking the button a second time another listener is added which does the same the first listener is doing. Therefore the listeners code will be executed twice. Clicking the button a 3rd, 4th, 5th ect. time will add more and more listeners to the property which will all be triggered on a change of the property.

The content of the list is removed on a click of the button, since you replace the old items list with a new empty one.

To simply add the selected item to the other table, it's sufficient to add the selected item to that table; no listener required.

Setting the cellValueFactorys should be done in the initialize method or in the fxml file:

@FXML
private void initialize() {
    colProduct2.setCellValueFactory(new PropertyValueFactory<productData, String>("name"));
    colKcal2.setCellValueFactory(new PropertyValueFactory<productData, String>("kcal"));
    colProtein2.setCellValueFactory(new PropertyValueFactory<productData, String>("protein"));
    colCarbs2.setCellValueFactory(new PropertyValueFactory<productData, String>("carb"));
    colFat2.setCellValueFactory(new PropertyValueFactory<productData, String>("fat"));
}

public void addMeal() {
    productData selection = tableProduct.getSelectionModel().getSelectedItem();

    if (selection != null) {
        // are you sure this needs to be copied???
        tableProduct2.getItems().add(new productData(selection.getName(), selection.getKcal(), selection.getProtein(), selection.getCarb(), selection.getFat()));
    }
}

Upvotes: 1

Related Questions