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

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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作