c0ol_jie 2016-03-23 08:19 采纳率: 33.3%
浏览 3672

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

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

急求,谢谢!!!

  • 写回答

1条回答

  • 栗超 2020-03-22 22:01
    关注
        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();
        }

    }

    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看