【问题标题】:JavaFX copying over a object from one tableview to another with a buttonJavaFX使用按钮将对象从一个tableview复制到另一个tableview
【发布时间】:2016-08-31 00:52:49
【问题描述】:

所以我正在为桌面游戏构建一个战团计算器,目前它看起来像这样

我想要做的是,当我单击“添加”按钮时,它会将该单位/生物复制到右侧的表格视图中(因此,如果我单击 Zombie 行上的“添加”按钮,一个僵尸被复制到右侧的 tableview 上)。

问题是,如果您选择行然后单击按钮,我只能使其工作,但我希望能够仅依靠按钮。我认为问题在于我在按钮类中使用“getSelectionModel().getSelectedItem()”来获取要复制的对象,但我找不到任何其他方法。

主类中的相关代码部分

//Creates the lists

  ObservableList<Unit> rightSideList = FXCollections.observableArrayList();

  ObservableList<Unit> leftSideList = FXCollections.observableArrayList();

  // Puts some test data in the lists

  public Main() {

      rightSideList.add(new Unit("Skeleton",5,4,9,4,1,6,9));
      rightSideList.add(new Unit("Ghoul",6,4,010,4,1,6,7));
      rightSideList.add(new Unit("Zombie",4,5,1,3,1,6,5));
      rightSideList.add(new Unit("Wraith",4,5,19,3,1,6,5));
      rightSideList.add(new Unit("Spectre",4,5,1,3,1,6,5));

      leftSideList.add(new Unit("Skeleton",5,4,0,4,1,6,9));

  }
          //Creates the tables

      final TableView<Unit> table1 = new TableView<>(
            rightSideList

              );
      final TableView<Unit> table2 = new TableView<>(     
              leftSideList

  );          
//Defines the table columns

      //Columns for table 1

      TableColumn<Unit,String> unitNameCol = new TableColumn<>("Unit");
      unitNameCol.setCellValueFactory(new PropertyValueFactory("unitName"));
      unitNameCol.setMinWidth(100);
      TableColumn<Unit,Integer> speedCol = new TableColumn<>("Spd");
      speedCol.setCellValueFactory(new PropertyValueFactory("speed"));
      TableColumn<Unit,Integer> meleeCol = new TableColumn<>("Me");
      meleeCol.setCellValueFactory(new PropertyValueFactory("melee"));
      TableColumn<Unit,Integer> rangedCol = new TableColumn<>("Ra");
      rangedCol.setCellValueFactory(new PropertyValueFactory("ranged"));
      TableColumn<Unit,Integer> defenseCol = new TableColumn<>("Def");
      defenseCol.setCellValueFactory(new PropertyValueFactory("defense"));
      TableColumn<Unit,Integer> attackCol = new TableColumn<>("Att");
      attackCol.setCellValueFactory(new PropertyValueFactory("attack"));
      TableColumn<Unit,Integer> toughnessCol = new TableColumn<>("To");
      toughnessCol.setCellValueFactory(new PropertyValueFactory("toughness"));
      TableColumn<Unit,Integer> costCol = new TableColumn<>("Cost");
      costCol.setCellValueFactory(new PropertyValueFactory("cost"));
      TableColumn<Unit, Boolean> actionCol = new TableColumn<>("Action");
        actionCol.setSortable(false);
        actionCol.setMinWidth(35);

            // define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows for table 1
        actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Unit, Boolean>, ObservableValue<Boolean>>() {
          @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Unit, Boolean> features) {
            return new SimpleBooleanProperty(features.getValue() != null);
          }
        });

        // create a cell value factory with an add button for each row in the table for table 1
        actionCol.setCellFactory(new Callback<TableColumn<Unit, Boolean>, TableCell<Unit, Boolean>>() {
          @Override public TableCell<Unit, Boolean> call(TableColumn<Unit, Boolean> unitBooleanTableColumn) {
            return new AddUnitCell(mainStage, table1);
          }
        });

这里是按钮类

/** A table cell containing a button for adding a unit */
  private class AddUnitCell extends TableCell<Unit, Boolean> {
    // a button for adding a new Unit.
    final Button addButton       = new Button("Add");
    // pads and centers the add button in the cell.
    final StackPane paddedButton = new StackPane();


    /**
     * AddUnitCell constructor
     * @param stage the stage in which the table is placed.
     * @param table the table to which a unit can be added.
     */
    AddUnitCell(final Stage stage, final TableView<Unit> table) {
      paddedButton.setPadding(new Insets(3));
      paddedButton.getChildren().add(addButton);

      addButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent actionEvent) {

            Unit selectedUnit = table.getSelectionModel().getSelectedItem();
            leftSideList.add(selectedUnit);
        }
      });
    }

    /** places an add button in the row only if the row is not empty. */
    @Override protected void updateItem(Boolean item, boolean empty) {
      super.updateItem(item, empty);
      if (!empty) {
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(paddedButton);
      } else {
        setGraphic(null);
      }
    }
  }

如果您需要详细检查,这里是项目存储库的链接

https://github.com/MagnusLindstrom88/Star-Struck-City-Warband-Calculator/blob/master/src/application/Main.java

【问题讨论】:

    标签: button javafx tableview


    【解决方案1】:

    代替

    Unit selectedUnit = table.getSelectionModel().getSelectedItem();
    

    在按钮事件处理程序的handle() 方法中,做

    Unit selectedUnit = table.getItems().get(getIndex());
    

    【讨论】:

      【解决方案2】:

      你应该从TableRow获取项目,而不是依赖选择模型来获取项目:

      Unit selectedUnit = (Unit) getTableRow().getItem();
      

      【讨论】:

        猜你喜欢
        • 2020-10-11
        • 2017-04-16
        • 2018-03-14
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多