JavaFX中如何在表格中添加一列按钮

使用JavaFX,想要使Tableview中的某一列 TableColumn 是按钮button ,该如何实现 ???

急求,谢谢!!!

    serverInfoButtonColumn.setCellFactory(tableColumn -> new AddServerInfoCell(serverInfoView));

package entity;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**

  • @title:
  • @author: LiChao
  • @dateTime: 2020/3/22 21:12
  • @description:
    /
    public class AddServerInfoCell extends TableCell {
    /

    • a button for adding a new person. */ final Button startButton = new Button("启动"); final Button delButton = new Button("删除");

    /**

    • records the y pos of the last button press so that the add person dialog can be shown next to the cell. */ final DoubleProperty buttonY = new SimpleDoubleProperty();

    /**

    • AddPersonCell constructor
    • @param table the stage in which the table is placed.
    • @param table the table to which a new person can be added. */ public AddServerInfoCell(final TableView table) { startButton.setOnMousePressed(mouseEvent -> buttonY.set(mouseEvent.getScreenY())); startButton.setOnAction(actionEvent -> { showAddPersonDialog(table, buttonY.get()); table.getSelectionModel().select(getTableRow().getIndex()); }); }

    /** 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);
    HBox pane = new HBox(startButton,delButton);
    pane.setAlignment(Pos.CENTER);
    setGraphic(pane);
    } else {
    setGraphic(null);
    }
    }

    /**

    • shows a dialog which displays a UI for adding a person to a table.
    • @param table the table to which a person is to be added.
    • @param y the y position of the top left corner of the dialog.
      */
      private void showAddPersonDialog(final TableView table, double y) {
      // initialize the dialog.
      final Stage dialog = new Stage();
      dialog.setTitle("New Person");
      dialog.initModality(Modality.WINDOW_MODAL); //必须关闭对话框后才能操作其他的
      dialog.initStyle(StageStyle.UTILITY); //对话框-只保留关闭按钮
      dialog.setY(y);

      // create a grid for the data entry.
      GridPane grid = new GridPane();
      final TextField firstNameField = new TextField();
      final TextField lastNameField = new TextField();
      grid.addRow(0, new Label("First Name"), firstNameField);
      grid.addRow(1, new Label("Last Name"), lastNameField);
      grid.setHgap(10);
      grid.setVgap(10);
      GridPane.setHgrow(firstNameField, Priority.ALWAYS);
      GridPane.setHgrow(lastNameField, Priority.ALWAYS);

      // create action buttons for the dialog.
      Button ok = new Button("OK");
      ok.setDefaultButton(true);
      Button cancel = new Button("Cancel");
      cancel.setCancelButton(true);

      // only enable the ok button when there has been some text entered.
      ok.disableProperty().bind(firstNameField.textProperty().isEqualTo("").or(lastNameField.textProperty().isEqualTo("")));

      // add action handlers for the dialog buttons.
      ok.setOnAction(actionEvent -> {
      int nextIndex = table.getSelectionModel().getSelectedIndex() + 1;
      table.getSelectionModel().select(nextIndex);
      dialog.close();
      });
      cancel.setOnAction(actionEvent -> dialog.close());

      // layout the dialog.
      HBox buttons = HBoxBuilder.create().spacing(10).children(ok, cancel).alignment(Pos.CENTER_RIGHT).build();
      VBox layout = new VBox(10);
      layout.getChildren().addAll(grid, buttons);
      layout.setPadding(new Insets(5));
      dialog.setScene(new Scene(layout));
      dialog.show();
      }

}