首页 > 解决方案 > How Can I Add TableView1 items to TableView2?

问题描述

The point that I'm looking for, is to getting tableview1 items and kind of coping it to tableview2 by selecting the tableview1 row and pressing the plus button ...

as you can see in the picture ...

enter image description here

enter image description here

I Have tried everything but the output that I get it's like this: enter image description here

my code is:

confirmbutton2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

            itemname2 =  tableView1.getSelectionModel().selectedItemProperty().get().toString() ;


            tableView1.getSelectionModel().selectedIndexProperty().addListener((num) -> {


                itemprice2 = tableView1.getSelectionModel().selectedIndexProperty().get();
                itemcount2 = tableView1.getSelectionModel().selectedIndexProperty().get();



            });

             tableview2.getItems().AddAll(new PersonTransfer(itemname2 , itemprice2 , itemcount2)) ;

标签: javajavafxtableview

解决方案


Check first selected item of TableView1 then add to TableView2. Please see the full TableView sample code in this answer.

public void onAddItem() {
    // check the table's selected item and get selected item
    if (table.getSelectionModel().getSelectedItem() != null) {
        Person selectedPerson = table.getSelectionModel().getSelectedItem();
        // add selected item to target table
        targetTable.getItems().add(selectedPerson);
    }
}

For second question: JavaFX is now modular and you can get JavaFX dependencies via Maven or Gradle. Desktop programming is always good and fun if you learn programming.


推荐阅读